PHP Cacher – Multi-purpose file caching library

PHP Cacher improve performance of website using caching techniques. PHP Cacher cache output of a PHP script and save it into html file. The data from this HTML file is used to output data to same [...]
PHP Cacher Multipurpose file caching solution

PHP Cacher Multipurpose file caching solution

Download source Contribute

PHP Cacher improve performance of website using caching techniques. PHP Cacher cache output of a PHP script and save it into html file. The data from this HTML file is used to output data to same requests. This method effectively reduces server downtime and increases performance. PHP Cacher also include options to avoid caching for unnecessary pages/requests. For example, PHP Cacher will not cache data if PHP session is already started. This option can override by setting disable_on_session to false.

PHP Cacher caching requests compariosn

PHP Cacher caching requests compariosn

Configurable Options

Option DataType Description Default Value
$cache Boolean Enable or disable caching for current request, If it is set to false it will disable caching for current request true
$location String The complete path to directory to which static html files to be saved, this directory should be writable true
$ttl Number Represent the time in seconds the files to be saved in cache or lifespan of cached html file. 3600
$request_id String The ID used for undentifying the request. This also represent how the they are saved in cache directory. This can be used to use particular part of a webpage to save information ‘url’
$disable_on_session Boolean Setting this variable true will disable caching when a PHP session is active. This can be used to disable caching after a particular user is logged in. true
$min_system_load Number The minimum system load at which cache to be activated. If it is set to a value above 0, the request is cached only if current system load is above $min_system_load 0
$request_types Array of strings The types of request to be cached. This can include Request types like “GET”,”POST”,”PUT” and “DELETE”. By default only GET requests will be cached. [“GET”]

PHP Cacher custom header shows how much older is current cache content is. This header does not included when current request is not loaded from cache

PHP-Cacher header custom cache header

PHP-Cacher header custom cache header

Usage Scenarios

1. Reduce System Load / Improve response Time

Some websites have high amount of processing to display a particular information on webpage. This can include database querys or data from another web services. This load might not be noticeable for a single request. But when number of requests become high this might affect serverload and response time. PHP Cacher will highly effective in thease cases to reduce server load and response time

2. Reduce downtime

Downtime of websites are in some cases caused by high rate of crawling by search engine bots. This may leads to high server load and downtime. Failed request for a website is considered as bad indication by search engines and pushes in search engine rankings. PHP Cacher allows ab option to enable caching only if server load is more than a specific value called $min_system_load. For example if this value is set as 0.50, PHP Cacher only use cached data if server load is above 0.5, if not it will disable caching and use normal updated output

3. Create Static Version of a website

PHP Cacher save cached data of a requests in a directory. The directory structure is created in such a way that it is similar to request url. So a static version of a website is created in static directory.

4. Help to cache a part of website

Most of the websites are created in a way that it displays user information in header and display a static content in body, this is static content is similar to all other pages and can be cached. This option in PHP Cacher is highly useful when dealing with a part of page which use high amount of resources and used on many pages. This is also useful sections like widgets which retrieve data from external APIs. You can use following example for the same.

// include phpcacher library
include "phpcacher.php";

* Content do not need to cache and vary for each requests

// Before static content start caching also specify a string to identify this section

* Cachable content block

// end caching after statc block completed

disable_on_session option is enabled by default. So if PHP session is already started before executing start function, the requests will not be cached.

How To Use

1. Include PHP-Cacher library file on your script.

Download source Contribute

include "phpcacher.php";

2. Configure static directory

Please make sure that you have configured static directory properly, The directory should be anywhere in the server, but you should make sure that it should be writable. Also please provide full directory path instead of relative directory path

PHPCacher::$location = "/var/www/static/";

3. Configure caching options as shown in above table.
4. Start caching.

As a final step the caching should be started by calling start() function of PHP Cacher.


Tagged with:

  • Rishikesh Krishnan

    Good One. Thanks for the article and hoping more from you soon. 🙂