Wednesday, 8 May 2013

How to use ZEND Cache with example

Zend_Cache provide an easy and convenient way for caching data in order to increase speed. However it will not be a good idea to cache data that is constantly being changed or updated.
Zend Framework uses front end and backend to caching.
Front end is responsible for operating cache while backend uses different adapters to store cache records.
Records/Data once caches can be easily removed/deleted when needed.
In order to use Zend_Cache, you will need to make some necessary configuration in your bootstrap.
Write the following code into your bootstrap file.
$frontend= array(
‘lifetime’ => 7200,
‘automatic_seralization’ => true
);
$backend= array(
‘cache_dir’ => ‘./tmp/’,
);
$cache = Zend_Cache::factory(‘core’
‘File’,
$fontend,
$backend
);
Zend_Registry::set(‘cache’,$cache);
In the above code, we first define two array, $frontend and $backend, which contain cache configuration information. Several other configuration options are available and can be define in these array’s. see Zend Framework documentation for available options.
Next we define/get an instance of Zend_Cache by calling its factory method.
The arguments passed to factory method are important.
Fist parameter say that we want to use Zend_Cache_Core class. We are not going into the details of the Core classs this time. Hopefully we will explain it when we discuss advance topics.
The second parameter describe that we want to use file system for the storage of our cache data.
Third and forth are $fontend and $backend options array.
The last line is compulsory-Zend_Registry::set(‘cache’,$cache), for the future use.
That’s it we have now configured our application to use Zend_Cache component.
We can now cache whatever data we like.
In order to use cache object stored in the bootstrap. Write the following code in your controller or model.
$cache = Zend_Registry::get(‘cache’);
if(!$result = $cache->load(‘mydata’)) {
echo ‘caching the data…..’;
$data=array(‘1’,’2’,’3’);
$cache->save($data, ‘mydata’);
} else {
echo ‘retrieving cache data…….’
Zend_Debug::dump($result);
}
In the first line we get reference of the cached object that was stored in the bootstrap.
Next we check for already cache data. If data is not cached the we cached it using save() method of the Zend_Cache. Here we are simply cache array containing 1,2,3. However you can fetch records from the database and store it as we store simple array.
If the data has already been cached, we go to the else part and dump the stored array.
As we have configured our $frontend array to cache data for two hour, so the data cached will automatically be remove after two hours.
However if later in your application you want to clean the entire cached data manually, simply write
$cache->clean();
This will clean the entire cached data. However if you want to remove specific data being cached, write
$cache->remove(‘mydata’);
This will clean the data we cached in our example.

No comments:

Post a Comment