Zend_Cache_Core is missing the setCacheDir method

This seems to be troubling a lot of people, so after debugging Zend’s code, I’ll post the solution on how to change the cache directory of zend_cache.

I’m using the below code in my zend framework bootstrap to setup Zend_Cache for caching to files.

$frontend= array('lifetime' => 7200,
				 'automatic_serialization' => true);
$backend= array('cache_dir' => DIR_CACHE);
$cache = Zend_Cache::factory('core',
//assign to registry                        
$registry     = Zend_Registry::getInstance();
$registry->cache = $cache;

I need to be able to override the cache directory at a later time and when I’m ready to use the cache in the application, I reference this Zend_Cache like below, which returns an object of Zend_Cache_Core, which you cannot call setCacheDir() on. We must first get the backend component, then call the method as below.

$cache = Zend_Registry::get('cache');
//set the new cache directory

Leave a comment

Your email address will not be published. Required fields are marked *