不止一个Zend_Cache配置?

时间:2012-05-05 18:39:41

标签: zend-framework zend-cache

是否可以为Zend_Cache设置两个不同的缓存实例? 例如,如果我想拥有一组永久存储的文件,除非我删除它们而另一组每分钟都会失效 - 我可以这样做吗?

Everywhere I look我总是看到只使用了一个前端和后端配置。

谢谢!

2 个答案:

答案 0 :(得分:3)

您只需创建两个不同的Zend_Cache实例,并将其粘贴到方便的位置。我做了类似这样的事情,在boostrap中实例化缓存实例,并将它们粘贴在注册表中,如下所示:

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;

  }

因此,Zend_Cache的设计实际上并没有限制您可以拥有的不同缓存的数量。您可以使用您喜欢的每个前端和后端独立配置它们。

答案 1 :(得分:-1)

您可以使用cachemanager资源:

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”是缓存键,您可以根据需要指定。

接下来我将cachemanager放到Zend_Registry(在Bootstrap中)

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

用法(无处不在)

Zend_Registry::get('Zend_Caches')->foo
相关问题