2

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!

JoeSlav
  • 4,479
  • 4
  • 31
  • 50
  • 1
    Couldn't you use different `cacheTemplate` through `Zend_Cache_Manager::setCacheTemplate()` rather than using different `Zend_Cache`? Not sure I'm entirely following your logic here. – tcole May 05 '12 at 18:53

2 Answers2

3

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.

timdev
  • 61,857
  • 6
  • 82
  • 92
-1

You can use cachemanager resource for instance:

resources.cachemanager.foo.frontend.name = Core
resources.cachemanager.foo.frontend.options.lifetime = 300
resources.cachemanager.foo.frontend.options.automatic_serialization = true
resources.cachemanager.foo.frontend.options.cache_id_prefix = some_prefix
resources.cachemanager.foo.backend.name = Memcached
resources.cachemanager.foo.backend.options.lifetime = 300

"foo" is cache key, you can specify as many as you want.

Next i put cachemanager to Zend_Registry (in Bootstrap)

protected function _initCache()
{
        Zend_Registry::set('Zend_Caches', $this->bootstrap('cachemanager')
                                               ->getPluginResource('cachemanager')
                                               ->getCacheManager());
}

Usage (everywhere)

Zend_Registry::get('Zend_Caches')->foo
Artur Michalak
  • 459
  • 3
  • 6