Example of Output Cache in Zend

To see how Page Cache works in zend . Here are the steps to implement Page Cache in Zend.

Step 1: Create a folder named “Cache” in your application public folder . Provide 777 permission on that folder so that it can store cached files.

Step 2:
Open index.php file and define the path where you want to store the cached files. By writing following code into index.php


defined('PUBLIC_PATH')

|| define('PUBLIC_PATH', realpath(dirname(__FILE__) . '/../public'));

Now paste following code on same file .


$time=60*60*24*1;

$key=md5($_SERVER['REQUEST_URI']);

$frontendOptions = array('lifeTime' =>$time,'automatic_serialization' => true);

$backendOptions = array('cache_dir' => PUBLIC_PATH.'/cache/');

$cache = Zend_Cache::factory('Output', 'File', $frontendOptions, $backendOptions);

//Uncomment below line if you want to clean the Cache
//$cache->clean(Zend_Cache::CLEANING_MODE_ALL);

if (!$cache->start($key) ){

$application->bootstrap()

->run();

//print "cache Miss: ";

$cache->end();

} else {

//echo "cache Hit: ";

}

Explanation of the code:
$time : its the timeline for cache. Above code represent that our cache will get updated after 1 days.

$key: Here we are creating unique code for current url. This url also included with querystring.

$frontendOptions and $backendOptions are already explained in Page Cache in Zend

$cache: We are initializing the cache now where first argument shows that its Output cache and and Cached files will stored in file.

In the below code it checks if current URL key is already available then it will show cached output.

Related posts:

The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video... Read More about Chandra Shekhar

Latest posts by Chandra Shekhar (see all)