Reputation: 7246
Is there a way to caching resultsets in Zend_db? For example, I want to run a select query using Zend_db and want this query to be cached to be able to run it faster later.
Upvotes: 4
Views: 3224
Reputation: 49
My advice is that create a initialization method in Bootstrap.php with prefix "_init". for exaple :
/**
*
* @return Zend_Cache_Manager
*/
public function _initCache()
{
$cacheManager = new Zend_Cache_Manager();
$frontendOptions = array(
'lifetime' => 7200, // cache lifetime of 2 hours
'automatic_serialization' => true
);
$backendOptions = array(
'cache_dir' => APPLICATION_PATH . '/cache/zend_cache'
);
$coreCache = Zend_Cache::factory(
'Core',
'File',
$frontendOptions,
$backendOptions
);
$cacheManager->setCache('coreCache', $coreCache);
$pageCache = Zend_Cache::factory(
'Page',
'File',
$frontendOptions,
$backendOptions
);
$cacheManager->setCache('pageCache', $pageCache);
Zend_Registry::set('cacheMan', $cacheManager);
return $cacheManager;
}
By this way, you have created and injected your cache manager with the caches which you need in your app. Now you can use this cache object where you want to use. For instance, in your controller or where else :
/**
*
* @return boolean |SimplePie
*/
public function getDayPosts()
{
$cacheManager = Zend_Registry::get('cacheMan');
$cache = $cacheManager->getCache('coreCache');
$cacheID = 'getDayPosts';
if (false === ($blog = $cache->load($cacheID))) {
$blog = Blog::find(array('order' => 'rand()', 'limit' => 1));
$cache->save($blog, $cacheID);
}
// do what you want to do with the daya you fetched.
}
Upvotes: 4
Reputation: 562881
You can use Zend_Cache when you want to save result-sets.
Zend_Db doesn't do any result-set caching itself. It's left for you to do it an application-specific way, because the framework has no way of knowing which result-sets need to be cached for performance reasons, versus those that can't be cached because you need them to be absolutely current. Those are criteria only you as the application developer know.
Just googling for "zend_db cache results" the first match is this blog showing how to use a Zend_Cache object to save a db query result: Zend Framework:: Caching the database query results
Upvotes: 2