Example of Page cache in Zend

To see how Page Cache works in zend . In the page cache actually it cache/store all the output of the whole page at the location we specify through our code. So when any user just open the page first time then with the help of page cache it cache the data at first time and from the next time onward it shows data from cached files. So page loading will be very fast next time. Here are the steps to implement Page Cache in Zend.

Step 1:

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'));

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

Step 3:

Just open Bootstrap.php file which exists on root of the application. Just paste following function in your class here


public function _initCache()

{

$frontendOptions = array(

'lifeTime' => 720,

'debug_header' => false,

);

$backendOptions = array(

'cache_dir' => PUBLIC_PATH.'/cache/'

);

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

$cache->start();

}

Explanation of above code: Here we are creating the function in the Bootstrap class so that whenever your application gets loaded the it will initiate the Zend_Cache as well.

$frontendOptions: In this variable we set up the application setting for Zend_Cache. In our example we are setting cache lifetime that means your cache will get reloaded after 720 seconds. Next setting is for cache debugger setting if it is yes that it will show on the page that whethe page is coming from cache or not.

$backendOptions: In this variable we setup the backend setting i.e., where we are going to store the cached files so here path has been defined. In our case we are storing cache in cache folder.

Now run your script and check whether cached files are created into cache folder or not. It you are able to see some files in your Cache folder then Caching is working fine.

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...