mpdonadio mpdonadio - 2 months ago 10
PHP Question

Getting all keys in a namespace with Zend Data Cache

With the Zend Data Cache - PHP API, is there a method for retrieving all keys in a namespace?

Essentially, I need to replicate what you can do with apc_cache_info, such as

$info = apc_cache_info("user");
$keys = array();

foreach ($info["cache_list"] as $entry) {
$keys[] = $entry["info"];
}


It is not apparent from the documentation if this is possible.

Thanks.

Answer

The Zend File Backend supports getTags() and getIds()

class Zend_Cache_Backend_File
{
    ....
    /**
     * Return an array of stored tags
     *
     * @return array array of stored tags (string)
     */
    public function getTags()
    {
        return $this->_get($this->_options['cache_dir'], 'tags', array());
    }
    /**
     * Return an array of stored tags
     *
     * @return array array of stored tags (string)
     */
    public function getTags()
    {
        return $this->_get($this->_options['cache_dir'], 'tags', array());
    }

In my bootstrap file i init the cache

protected function _initCache()
{
    $frontendOptions = array(
        'lifetime' => 3600*24*5, // cache lifetime of 5 days
        'automatic_serialization' => true,
        'logging' => false,
        'caching' => true
    );

    $backendOptions = array(
        'cache_dir' => './../data/cache/', // Directory where to put the cache files
        'hashed_directory_level' => 2
    );

    // getting a Zend_Cache_Core object
    $cache = Zend_Cache::factory(
        'Core',
        'File',
        $frontendOptions,
        $backendOptions);
    Zend_Registry::set('cache', $cache); 

Then in my controller i can call

public function indexAction()
{
    $cache = Zend_Registry::get('cache');
    Zend_Debug::dump($cache->getTags());
    Zend_Debug::dump($cache->getIds());

Suggest you check the Zend code for the specific cache backend your using.