我一直在关注this tutorial使用ZF2 DoctrineORMModule(版本1.0.0)设置doctrine(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 ...
我创建了issue in the DoctrineORMModule repo,但我想我可能错过了教程中未提及的一些必要设置。
答案 0 :(得分:1)
我似乎回想起试图使用Doctrine Redis缓存的同一块砖墙。相反,我选择使用Zend Cache适配器来使Doctrine Cache在ZF2模块中工作。
我是这样做的。
首先创建一个Zend Cache工厂。下面是我写的一个,所以我的缓存现在可以配置而不是硬编码。如果您需要更改缓存服务器甚至缓存引擎,这还有额外的好处,您只需更新配置即可。如果您使用多个环境(开发/暂存/生产),这将非常有用。
模块/应用/工厂/ 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'],
]
);
}
}
接下来创建一个Doctrine Cache Factory,这会将您配置的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 Framework Documentation
中找到所有可用的Zend Cache适配器及其设置我知道这对你的教程没有帮助,但我希望这对你有用,让你开始运作。