Search code examples
zend-frameworkzend-cache

More than one Zend_Cache configuration?


Is it possible to have two different cache instances for Zend_Cache? For instance, if I wanted to have a set of files that are stored forever unless I delete them and another set that gets invalidated every minute -- could I do that?

Everywhere I look I always see only one frontend and backend configuration used.

Thanks!


Solution

  • You simply create two different instances of Zend_Cache and stick them somewhere handy. I did something like this once, instantiating the cache instances in the boostrap, and just sticking them in the registry, like this:

    protected function _initCache(){
        $this->bootstrap('config');
        $config = Zend_Registry::get('config')->myapp->cache;
    
        $cache = Zend_Cache::factory(
          $config->frontend->name,
          $config->backend->name,
          $config->frontend->opts->toArray(),
          $config->backend->opts->toArray()
        );
        Zend_Registry::set('cache',$cache);
        return $cache;
      }
    
      protected function _initUserCache(){
        $this->bootstrap('config');
        $config = Zend_Registry::get('config')->myapp->othercache;
    
        $cache = Zend_Cache::factory(
          $config->frontend->name,
          $config->backend->name,
          $config->frontend->opts->toArray(),
          $config->backend->opts->toArray()
        );
        Zend_Registry::set('othercache',$cache);
        return $cache;
    
      }
    

    So, there's really nothing about the design of Zend_Cache that limits the number of different caches you can have. You can configure them all independently, using whatever front-end and back-end you like for each.