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.
Chandra Shekhar
Latest posts by Chandra Shekhar (see all)
- Best practices for micro service design - January 23, 2022
- Spring Boot - January 23, 2022
- Java - January 23, 2022
Recent Comments