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.

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 Conferencing and social media domain. The motive of creating cutehits was just to share the knowledge/solutions I get to know during my day to day life so that if possible I can help someone for same problems/solutions. CuteHits.com is a really a very effort for sharing knowledge to rest of the world. For any query/suggestion about same you can contact me on below details:- Email: shekharmca2005 at gmail.com Phone: +91-9560201363

Latest posts by Chandra Shekhar (see all)

You may also like...