Caching is basically used to improve the performance of the application. Your application may consists of multiple expensive queries ruining the application performance. The Zend_Cache component of Zend Framework provides a good feature to improve the page performance by caching the database query results. Here is the sample code :
// Creating the cache options
$frontend = array(
‘lifetime’ => 86400, // cache lifetime of 24 hours (time is in seconds)
‘automatic_serialization’ => true //default is false
);$cachedir = /path/to/cacheDirectory;
if(!is_dir($cachedir)){
mkdir($cachedir,0755);
}$backend = array(‘cache_dir’ => $cachedir);
// Getting a Zend_Cache_Core object
$zend_cache = Zend_Cache::factory(‘Core’, ‘File’, $frontend, $backend);$result = run_query($zend_cache);
print_r($result);function run_query($cacheObj){
$q = “Database query”;
if(!$result = $cacheObj->cache->load(“myresult”)){
$db = Zend_Db::factory(DB_connection_parameters);
$result = $db->fetchAll($q);
$cacheObj->save($result, “myresult”);
}
else {
echo “This result is from cache.”;
}
return $result;
}
Caution: You should give the unique name in the “load(‘name’) and save(‘name’)” functions.
Otherwise you will get the same result on the pages where the query is intended to produce
different results.