Zend_db caching

Is there a way to cache result sets in Zend_db? For example, I want to run a select query using Zend_db and want this query to be cached in order to run it faster.

+4
source share
2 answers

My advice is to create an initialization method in Bootstrap.php with the 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; } 

So you created and introduced your cache manager with the caches you need in your application. Now you can use this cache object that you want to use. For example, in your controller or somewhere 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. } 
+4
source

You can use Zend_Cache when you want to save result sets.

Zend_Db does not cache the result itself. This allowed you to do this taking into account specific applications, because there is no way in the structure to find out which result elements need to be cached for performance reasons, compared to those that cannot be cached, because you need them to be absolutely current. These are the criteria that you, as the application developer knows.

Just a search on "zend_db cache results", the first match is a blog showing how to use the Zend_Cache object to save the result of a db query: Zend Framework :: Caching database query results

+2
source

Source: https://habr.com/ru/post/1394807/


All Articles