How to use Catch_Lite?

Objective of the blog

After reading this blog, you will be able to understand how cache_lite is used to create cache for the HTML.

Let’s know about the Cache_Lite

Cache_Lite is a caching system which can be used for high traffic website. The simple logic of cache lite is to serve the HTML from cache if cache is already created for that HTML so that time to fetch/prepare from the database/code execution for creating the HTML can be saved.

Cache_Lite creates cache of HTML for a page on basis of an id when request of that page comes first time and simply serve the page first time without cache. Now, when request for the same page will come again, HTML will be simply fetched from the cache corresponding to the id and will show the page i.e. system will not create HTML again and will throw the HTML from cache.

Checking before Cache_Lite Implementation

Before implementing cache_lite, you need to check whether PEAR is installed or not. For this, create a file in the root and save the below lines of code in that file and run the same on browser.

require_once 'Cache/Lite.php';
var_dump(class_exists('Cache_Lite', false));

If you get boolean true in response, the PEAR is already installed and have Cache_Lite package. But if you get boolean false or any other error in the response, you further need to check whether PEAR is not installed or PEAR does not have Cache_lite package. For this create a file in the root and save the below lines of code in that file and run the same on browser.

require_once 'System.php';
var_dump(class_exists('System', false));

If you get boolean true in response, the PEAR is installed but PEAR does not have Cache_Lite package. In this case, please refer If you still get boolean false or any other error, either PEAR is not installed or include_path is not set. In this case, please refer

Cache_Lite Implementation

If PEAR is installed and have Cache_Lite package, you can start implementing the Cache_Lite. The process to create cache lite is as below:

  1. Firstly, include Cache_Lite package. For this require Cache/Lite.php file.
  2. Create an id which should be unique for that HTML so that it can be identified in the cache folder at the time of fetch from cache. Encrypt this id using md5.
  3. Define the directory where you want to save cache and refresh time after which you want to refresh the cache. If you want to refresh cache once in a day then set 86400. Now when a page is opened first time, cache will be created and that page will be served by cache for next 24 hour (this 24 hour will be calculated after creating cache). Now cache will be refreshed only when page will be loaded again after 24 hour.
  4. After that, create an object of cache_lite and check whether cache is created for id or not. If cache is created, uncompress the cache data and load the data on browser. And if cache is not created, get data from output buffer, compressed this data and save in cache.

For more understanding, please look the code snippet written below:

define('CACHE_LITE_STATUS', 'ON'); //Set other than ON if you don't want to serve HTML from cache
define('CACHE_LITE_REFRESH_TIME', '43200'); //This is cache lifetime after which you want to refresh cache.
    $id = 'testid';//This is the unique id using which cache will be created.
    $id = md5($id);
    $options = array(
        'cacheDir' =>  "testDir/",//testDir is the directory where cache will be saved. Cache will be created with the same name as id so director name should be with a trailing slash at the end
        'lifeTime' => CACHE_LITE_REFRESH_TIME
    $Cache_Lite = new Cache_Lite($options);
    if ($data = $Cache_Lite->get($id)) {
        echo gzuncompress($data); //To uncompress a compressed string. This is to uncompress the data fetched from cache
        ob_start(); //Turn on output buffering
        $data = ob_get_contents(); // This will return the contents of the output buffer
        $data = gzcompress($data, 9); //To compress output buffer data
        $Cache_Lite->save($data); //To save the compressed data in the cache
        ob_end_flush(); //Flush the output buffer and turn off output buffering


After reading this document you can try the same code on your system to serve the HTML using Cache_Lite. Please let us know for further improvement.

Arihant Jain

Arihant is a highly experienced PHP developer and also have good knowledge of database designing. He believes to spread the knowledge in the simple form which he learned after reading a lot of stuff and/or facing many struggles so he keeps himself always interested in writing blogs.

Leave a Reply

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