Zend Framework · February 8, 2012 0

Zend framework Cache

Caching have a phenomenal effect on server reducing load and better utilizing resources.

Zend framework provides Zend_Cache as a generic way to cache any data. You may cache sql queries, objects, variables, html contents etc.
Using Zend_Cache is fairly simple like

Saving a content into cache

$cache->save($content, "content_key");

getting saved/cached contents

$content = $cache->load("content_key");

here we configure zend framework application to work with cache

open your bootstrap file and add a function as below

public function _initCache()
{
	$frontendOptions = array (
		'lifetime' => 30,   //cache it for 30 seconds
		'automatic_serialization' => true
		);
	
	$backendOptions = array (
                //assumes a a directory exists in following path
		'cache_dir' => APPLICATION_PATH . '/../data/cache/'
	);
	
	$cache = Zend_Cache::factory('Page', 'File', $frontendOptions, $backendOptions);
	Zend_Registry::set('cache', $cache);
	
}	

In your application you can use Zend_Cache as

$cache = Zend_Registry::get('cache');

$cahce->load('content_key');