PHP Fast CachePHP TutorialsWeb Development

PHPFastCache usage for increasing page load speed with example

This web guide will show you PHPFastCache usage for increasing page load speed with example . Today, website development is not a difficult task but what to do if your website speed is getting slow. If you are also facing this same problem and you do not want to lose your visitors because of website speed then read out this full article.  It is mandatory for every website owner to enable some caching system then that will save page load time on your website. PHP Fast Cache is one the popular cache system that can help you for boosting website. It is faster then all other website boosting libraries.

Steps to use PHPFastCache in your website

I have divided procedure for using phpfastcache in 3 parts that are installing Library, Developing test file and Running PHPFastCache. We will start from installing PHP Fast Cache library then move on to other parts sequentially.

Installing PHP Fast Cache Library

  1. Go to and download phpfastcache library.
  2. After downloading zip file extract it anywhere in your operating system.
  3. Create a folder in your website root directory either you are using localhost or online server after that rename the folder with any name you want. In my case folder name is booster.
    PHPFastCache Folder
  4. Now copy all extracted files in that folder you created in step 3 as shown belowPHP Fast cache files

Creating test file for using PHP Fast Cache

  1. Create a file with extension .php in your website root directory then open it on any editor as shown below
    PHP Fast Cache test file
    In my case I have created file index.php which I have open in Sublime text. You can choose any editor which you like.
  2. Once you have open file in editor our first task will be to include phpfastcache library main file that is autoload.php as show below
    //Getting PHP Fast Cache Library
  3. After including library you have to configure PHP Fast cache for using cache like Memcache credentials, Redis credentials, files directory etc. PHPFast cache support Redis, APC, XCache, MemCached, WinCache, Cookie, Predis, SSDB, MongoDB and files etc. Don’t be panic not all server supports all cache, just ask your hosting provide about all cache system mention above before configuring.
  4. Add the following code in your file
    //Configuring PHP Fast Cache
    	//Set it auto
    	"storage"   =>  "auto", // auto, files, sqlite, apc, cookie, memcache, memcached, predis, redis, wincache, xcache
    	"default_chmod" => 0777, // For security, please use 0666 for module and 0644 for cgi.
    	// create .htaccess to protect cache folder 
    	"htaccess"      => true,
    	//Path cached Data will save
    	"path"      =>  dirname(__FILE__)."/files",
    	"securityKey"   =>  "auto", // auto will use domain name, set it to 1 string if you use alias domain name
    	// MEMCACHE
    	"memcache"        =>  array(
    		array("You Ip",11211,1),
    		//  array("",11211,1),
    	// REDIS
    	"redis"         =>  array(
    		"host"  => "YourIp",
    		"port"  =>  "",
    		"password"  =>  "",
    		"database"  =>  "",
    		"timeout"   =>  ""
    	 * If defined storage not worked then call files storage
    	"fallback"  => "files",

    PHP Fast Cache has various options that can you set it up. I have only mention important one’s that are mandatory to set up.

    • storage : Set it to auto if your do not know which cache modules are available in your server. I am using localhost so I set it “auto”. Storage mean configuring PHP Fast Cache for using particular storage like redis, memcached etc.
    • default_chmod : Set permissions for cached files folder  if you have choose storage files.
    • htaccess :  Set it true for enabling security of cached files folder so no one can directly access to folder.
    • path : Set path where all cached files will be store. In my case files is the folder name in root directory of website.
    • securityKey : Change it from auto to your domain name if you do not want to share cached files with other domains.
    • memcache : Here you can add memcache credentials that are provided by your hosting provider.
    • redis : Here you can add memcache credentials that are provided by your hosting provider.
    • fallback : If any of the storage stop working then it will call fallback storage. If memcached stop working then whatever storage you define in fallback will start working without showing any error.
  5. After configuring PHPFastCache add following code
        $cache = \phpFastCache\CacheManager::getInstance();
        // Getting Cached data with key
        $string = $cache->getItem("--yourkey--");
        //If cached data is not set then setting up data for defined time
        if (is_null($string->get())) { 
    	    $string->set("---your data will be here---")->expiresAfter(5);//in seconds, also accepts Datetime
    		$cache->save($string); // Save the cache item just like you do with doctrine and entities
    	    echo "Data without saved.";
    	    echo $string->get();
    	} else {
    	    echo "Saved data ";
    	    echo $string->get()[0];// Will prints 'First product'
        //Printing data
        echo $string->get();

    Remember add above code below configuration that you set up in step 4.

Running PHP Fast Cache

Before running php fast cache let’s discuss code first. In part 2 step 5 we define a variable $cache in which we call PHP Fast cache instance. After we define a variable $string in which we will try to get cached data using key “–yourkey–” , you can change that key with yours but be sure that every key will be different for different data. For example, you have product A and product B then for product A data key will be p1 and for product B data  key will be p2. After that we will use if statement to check that cached data is empty or not. If cached data return empty then we set some data in it. In above code replace

—your data will be here—

with your data you want to store in cached storage. Replace expire after time with your time in seconds. Then try to get same data before that add a line with php echo command “Data without saved” to differentiate between save data and unsave data. Save file and open it in browser.

When file run first time it will show

PHP Fast Cache Example

And when you refresh browser window then it will show

PHP Fast Cache Example


—your data will be here—

with any data like your sql queries data etc. It will refresh data only when the time period you set in seconds will expire. In my case I have set it to 5 seconds so after 5 seconds PHP Fast cache will again set data for particular key.

Hope, this is web guide is very helpful for you. For any questions comment down below.

Anil Mehra

I am Anil Mehra, a passionate, workaholic and a Full Stack Tech Savvy Programmer with true north towards growth. I have worked on 256 live projects in MNC. I am expertise in the field of Programming, Server Management, SEO, Blogging and SMO...

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button

Adblock Detected

Please turn off Ad blocker to view website