在ZF2项目中启用Doctrine 2缓存

use*_*183 4 php caching zend-framework doctrine-orm zend-framework2

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

在这里,我尝试过,但在时间执行中添加的时间内看不出任何差异

模块\应用\设置\ 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',
            )
        )
    ),
Run Code Online (Sandbox Code Playgroud)

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

edi*_*igu 8

为减少不必要的麻烦,请始终array在开发时使用缓存memcached,redis或者apc在生产环境中运行应用程序时使用缓存.

您应该将工厂定义放在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;
            },
        ],
    ],
];
Run Code Online (Sandbox Code Playgroud)

此外,我强烈建议您始终将工厂搬到各个工厂类别.这样,借助合并配置缓存,您将在生产环境中拥有更具可读性,可维护性和高效率的应用程序.

例如:

'service_manager' => [
    'factories' => [
        'doctrine.cache.mycache' => \App\Memcache\Factory::class // implement FactoryInterface
        ],
    ],
];
Run Code Online (Sandbox Code Playgroud)