Zend_Cache Dummy backend
Posted on Friday, November 27th, 2009 under zend, zend framework, zend_cacheI was working today on a caching system for one of our projects written using Zend_Framework. And, of course, in order to see what I was doing I needed to turn off the cache in the development stage. A task that proved to be more complicated that it would seem, as Zend_Cache doesn’t have a “turn off” feature. As pointed out by robo in this comment, Zend_Cache *does* have a turn off feature. Documented. There! On the first paragraph of the first page! Man l feel dumb
Well…live and learn. Special thanks go to Robo47 for pointing out my error.
Setting the “caching” attribute to false in the caching frontend will stop Zend_Cache. Use this in your bootstrap:
/** * inits the cache * * @return void */ protected function _initCache() { $frontendOptions = array( 'lifeTime' => 60, 'automatic_serialization' => true, ); $backendOptions = array( 'cache_db_complete_path' => APPLICATION_PATH . '/cache/cache.sqlite', 'automatic_vacuum_factor' => 5, ); if(APPLICATION_ENV != 'development') { $frontendOptions['caching'] = false; } $cache = Zend_Cache::factory('Core', 'Sqlite', $frontendOptions, $backendOptions); Zend_Registry::set('cache', $cache); }
Simple as that. But if you don’t have anything better to do now, you can read original post (non edited) below and see how complicated a simple thing can get:
After looking dumb to the screen for several minutes – hoping that the problem will fix itself or something – I came to the conclusion that the easiest way to turn off the cache would be, in my case, to use a dummy cache backend, that doesn’t cache anything and always returns false when you try to fetch an object from it. Just like django has.
But Zend Framework doesn’t have such a class. It has a caching backend class called Zend_Cache_Backend_Test, but its method return stuff like ‘foo’ and ‘bar’. Really! They do! I don’t get this class’s purpose, as it’s clear that it can’t be used for testing. So I’ve decided to write my own dummy caching backend class and share it with my readers. It can be downloaded directly from my GitHub repository, by following this link Generic_Cache_Dummy.php.
The usage is quite simple, just add this to your bootstrap:
/** * inits the cache * * @return void */ protected function _initCache() { $frontendOptions = array( 'lifeTime' => 60, 'automatic_serialization' => true, ); $backendOptions = array( 'cache_db_complete_path' => APPLICATION_PATH . '/cache/cache.sqlite', 'automatic_vacuum_factor' => 5, ); if(APPLICATION_ENV != 'development') { $cache = Zend_Cache::factory('Core', 'Sqlite', $frontendOptions, $backendOptions); } else { $dummyBackend = new Generic_Cache_Dummy(); $cache = Zend_Cache::factory('Core', $dummyBackend, $frontendOptions, $backendOptions); } Zend_Registry::set('cache', $cache); }
…and, whenever you need to save/retrieve something from the cache, just do…
$cache = Zend_Registry::get('cache'); if(!($object = $cache->load($cacheKey))) { // bla bla bla...fetch object $cache->save($object, $key); } return $object;
Happy caching!
You know the saying: if debugging means taking the bugs out, then programming means putting them in. Yes. We all have bugs in our code. And since not all of them can be marketed as “undocumented features”, from time to time we have to debug our applications.
I’ve downloaded the 1.8 version of Zend Framework and looked over Zend_Tool these last days. What can I say. What is there to say. Zend_Tool is cvasi-useless. You can use it to create a new project, as a shortcut to
I’m going to hold a presentation about Zend Framework at PHP Geek Meet in Cluj, Romania. Everyone’s invited.