条令redis缓存:选项“;query_cache";没有可调用的“;setQueryCache”;


Doctrine redis caching: The option "query_cache" does not have a callable "setQueryCache"

我一直在遵循本教程,使用ZF2 DoctrineORMModule(1.0.0版)设置条令(2.5)redis二级缓存。

然而,我得到以下错误:

Fatal error: Uncaught Zend'Stdlib'Exception'BadMethodCallException: 
The option "query_cache" does not have a callable 
"setQueryCache" ("setquerycache") setter method which must be defined in ...

我在DoctrineORMModule repo中创建了一个问题,但我认为我可能错过了教程中没有提到的一些必要设置。

我似乎记得在尝试使用Doctrine Redis缓存时遇到过同样的砖墙。相反,我选择使用ZendCache适配器来让DoctrineCache在ZF2模块中工作。

以下是我的做法。

首先创建一个ZendCache工厂。下面是我写的一个,所以我的缓存现在是可配置的,而不是硬编码的。这还有一个额外的好处,如果您需要更改缓存服务器甚至缓存引擎,只需更新配置即可。如果您有多个工作环境(开发/暂存/生产),这将非常有效。

模块/Application/Factory/CacheFactory.php

<?php
namespace Application'Factory;
use Zend'Cache'Storage'StorageInterface;
use Zend'Cache'StorageFactory;
use Zend'ServiceManager'FactoryInterface;
use Zend'ServiceManager'ServiceLocatorInterface;
class CacheFactory implements FactoryInterface
{
    /**
     * Creates Service
     *
     * @param ServiceLocatorInterface $serviceLocator Zend Service Locator
     *
     * @return StorageInterface
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $config = $serviceLocator->get('config');
        return StorageFactory::factory(
            [
                'adapter' => [
                    'name' => $config['cache']['adapter'],
                    'options' => $config['cache']['options'],
                ],
                'plugins' => $config['cache']['plugins'],
            ]
        );
    }
}

接下来创建一个DoctrineCache工厂,这将把您配置的Zend Cache服务放入DoctrineModule''Cache''ZendStorageCache适配器中。

模块/应用程序/工厂/DoctrineCacheFactory.php

<?php
namespace Application'Factory;
use DoctrineModule'Cache'ZendStorageCache;
use Zend'ServiceManager'FactoryInterface;
use Zend'ServiceManager'ServiceLocatorInterface;
class DoctrineCacheFactory implements FactoryInterface
{
    /**
     * Create Service
     *
     * @param ServiceLocatorInterface $serviceLocator Zend Service Manager
     *
     * @return ZendStorageCache
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        /** @var 'Zend'Cache'Storage'StorageInterface $zendCache */
        $zendCache = $serviceLocator->get('Application'Service'Cache');
        return new ZendStorageCache($zendCache);
    }
}

最后,将配置中的所有内容连接起来。哪种配置取决于您的环境。config/autoload/local.php可能是一个很好的起点。

<?php
return [
    'cache' => [
        'adapter' => 'Zend'Cache'Storage'Adapter'Redis',
        'plugins' => ['Serializer'],
        'options' => [
            'server' => array(
                'host' => '127.0.0.1',
                'port' => 6379,
                'timeout' => 300,
            ),
            'namespace' => 'application_cache'
        ]
    ],
    'service_manager' => [
        'factories' => [
            'Application'Service'Cache' => 'Application'Factory'CacheFactory',
            'doctrine.cache.doctrine_cache' => 'Application'Factory'DoctrineCacheFactory',
        ],
    ],
    'doctrine' => [
        'configuration' => [
            'orm_default' => [
                'query_cache'       => 'doctrine_cache',
                'result_cache'      => 'doctrine_cache',
                'metadata_cache'    => 'doctrine_cache',
                'hydration_cache'   => 'doctrine_cache',
                'second_level_cache' => [
                    'enabled'               => true,
                    'default_lifetime'      => 200,
                    'default_lock_lifetime' => 500,
                    'file_lock_region_directory' => __DIR__ . '/../my_dir',
                    'regions' => [
                        'My'FirstRegion'Name' => [
                            'lifetime'      => 800,
                            'lock_lifetime' => 1000
                        ],
                        'My'SecondRegion'Name' => [
                            'lifetime'      => 10,
                            'lock_lifetime' => 20
                        ],
                    ],
                ],
            ],
        ],
    ],
];

所有可用的Zend缓存适配器及其设置都可以在Zend Framework文档中找到

我知道这对你的教程没有帮助,但我希望这对你有用,让你开始学习。