【问题标题】:Zend Framework 2 - AdaptersZend Framework 2 - 适配器
【发布时间】:2013-02-22 11:41:41
【问题描述】:

我是 Zend Framework 的新手,我有一个问题。

我的 CMS 中有 20 个模块,所有模块都使用 APC 缓存适配器(在模块配置中定义),现在我想将此适配器更改为 Memcache。

有什么好的做法而不是改变所有的 config.module.php 吗?

【问题讨论】:

    标签: zend-framework2


    【解决方案1】:

    如果您使用 DI/Service Manager 设置缓存/缓存适配器,那么您应该能够非常简单地通过更改 Service Manager Config 中的定义来更改它。

    服务管理器配置:

        'My\Cache' => function($sm){
            $cache = \Zend\Cache\StorageFactory::factory(array(
                'adapter' => 'filesystem',
                'plugins' => array(
                    'exception_handler' => array('throw_exceptions' => FALSE),
                    'serializer'
                )
            ));
    
            $cache->setOptions(array(
                'cache_dir' => './data/cache',
                'ttl'       => 60*60,
            ));
    
            return $cache;
        },
    

    然后您可以在 SM 中更改适配器和设置。所有模块都将使用服务管理器来使用缓存,因此它们不需要进一步更改。

    一些模块:

    <?php $cache = $this->getServiceManager()->get('My\Cache'); ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多