php - 在 ZF2 项目中启用 Doctrine 2 缓存

标签 php caching zend-framework doctrine-orm zend-framework2

如何在使用 Zend Framework 2 和 Doctrine 2 的项目中启用缓存?究竟应该启用什么缓存,doctrine 缓存还是 zend 缓存?

这是我尝试过的,但在

中添加的执行时间上看不到任何差异

module\Application\config\module.config.php

 'doctrine.cache.my_memcache' => function ($sm) {
            $cache = new \Doctrine\Common\Cache\MemcacheCache();
            $memcache = new \Memcache();
            $memcache->connect('localhost', 11211);
            $cache->setMemcache($memcache);
        return $cache;
      }, 


    'doctrine.cache.apc' => function ($sm){
            $apc = new \Doctrine\Common\Cache\ApcCache();
            return $apc;
    },



    // Doctrine config
    'doctrine' => array(
        'driver' => array(
            __NAMESPACE__ . '_driver'   => array(
                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'cache' => 'array',
                'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity'),
            ),
            'orm_default' => array(
                'drivers' => array(
                    __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
                ),
            )
        ),
        'configuration' => array(
            'orm_defaults' => array(
                'metadata_cache'    => 'apc',
                'query_cache'       => 'apc',
                'result_cache'      => 'my_memcache',
            )
        )
    ),

任何帮助或想法或解释表示赞赏。 谢谢。

最佳答案

为了减少不必要的麻烦,在开发时总是使用array 缓存,而在开发时总是使用memcachedredisapc您的应用程序在生产环境中运行。

您应该将工厂定义放在 service_manager > factories 键下,而不是直接放在模块配置数组中。

在你的 module.config.php 中试试这个:

return [
    'doctrine' => [
        'configuration' => [
            'orm_default' => [
                'metadata_cache' => 'mycache',
                'query_cache' => 'mycache',
                'result_cache' => 'mycache',
                'hydration_cache' => 'mycache',
            ]
        ],
    ],

    'service_manager' => [
        'factories' => [
            'doctrine.cache.mycache' => function ($sm) {
                $cache = new \Doctrine\Common\Cache\MemcacheCache();
                $memcache = new \Memcache();
                $memcache->connect('localhost', 11211);
                $cache->setMemcache($memcache);

                return $cache;
            },
        ],
    ],
];

此外,我强烈建议始终将工厂移动到单独的工厂类。这样,在 merged configuration cache 的帮助下,您将在生产环境中拥有更具可读性、可维护性和效率的应用程序。 .

例如:

'service_manager' => [
    'factories' => [
        'doctrine.cache.mycache' => \App\Memcache\Factory::class // implement FactoryInterface
        ],
    ],
];

几年后为 future 的读者更新:

我强烈建议查看 roave/psr-container-doctrine 在为实体管理器、配置或缓存等各种学说组件编写自定义专用工厂之前。作为图书馆的贡献者,我可以说它很好地满足了我目前需要的大多数用例的目的。当你真的需要一个专门的工厂时,你可以扩展或组合或装饰提供的工厂,并在上面添加你自己的逻辑。

关于php - 在 ZF2 项目中启用 Doctrine 2 缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26364070/

相关文章:

javascript - 使用 Zend Form 专注于文本区域

unit-testing - phing 和 phpunit + bootstrap

javascript - OWL 轮播在 WordPress 主题中无法以 RTL 格式工作

php - 通过 PUT 方法上传 Symfony REST 文件

elasticsearch - 为什么我的elasticsearch查询缓存是空的?

ios - 如何在 swift 5 中缓存图像?

php - 我怎样才能扩展 Zend_Controller_Action 使一个函数在所有 Controller 中通用

php - 缓存 AJAX 请求

php - 根据平均值分配行

java - infinispan缓存服务器过期失败