Caching data using Zend_Cache with example

1 Dec

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.

About these ads

6 Responses to “Caching data using Zend_Cache with example”

  1. Shaun March 17, 2009 at 1:33 am #

    I keep getting an error that says,

    exception ‘Zend_Cache_Exception’ with message ‘cache_dir must be a directory’

    You have any idea what this could be?

  2. Faheem Abbas March 17, 2009 at 6:16 am #

    Give correct directory path.

  3. Shaun March 17, 2009 at 10:42 am #

    I created the folder through Zend Studio and that caused problems. deleted it then created it through a terminal window and it worked just fine.

  4. Amit Shah May 1, 2009 at 6:25 am #

    If we stored cache data for two hour than should that data cached by particular session only for two hours or it would cached for all sessions.

    What I mean is, X user browse one page at 1:00pm and Y user browse same page at 2:00pm. That page is storing ‘mydata’ in cache using $cache->save($data, ‘mydata’);.

    Now my question is, does both users mydata cache will deleted by 3:00 pm? Or X users mydata will be removed by 3:00pm and Y users mydata will be removed by 4:00pm.

  5. Faheem Abbas May 1, 2009 at 11:50 am #

    X’s cache will be cleared at 3:00 and Y’s at 4:00?
    No, cache should be cleared at 3:00 regardless of X and Y.

  6. ron January 11, 2010 at 9:54 am #

    hi

    how can i cache and then pass the cache version to the zend paginator to display the cached version?

    i`m stuck with this problem forever. can someone please help?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 33 other followers

%d bloggers like this: