要在我的 Symfony2 应用程序中使用 Redis缓存,我按照此tutorial(法语,抱歉!)。
除了我使用的实体监听器 Doctrine 之外的所有内容以及用于在数据库写入时更新我的缓存的事件......我有这样的服务:
<?php
namespace Maxpou\BeerBundle\Service;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Maxpou\BeerBundle\Entity\Beer;
class BeerListener
{
private $cacheDriver;
public function __construct($cacheDriver)
{
$this->cacheDriver = $cacheDriver;
}
public function postPersist(Beer $beer, LifecycleEventArgs $args)
{
$this->cacheDriver->expire('[beers_all][1]', 0);
}
public function postUpdate(Beer $beer, LifecycleEventArgs $args)
{
$this->cacheDriver->expire('[beers_all][1]', 0);
}
public function postRemove(Beer $beer, LifecycleEventArgs $args)
{
$this->cacheDriver->expire('[beers_all][1]', 0);
}
}
在我的config.yml中,我添加:
beer_listener:
class: Maxpou\BeerBundle\Service\BeerListener
arguments:
- "@snc_redis.doctrine"
tags:
- { name: doctrine.orm.entity_listener }
在我的实体中,我添加了以下注释:
* @ORM\EntityListeners({"Maxpou\BeerBundle\Service\BeerListener"})
当我尝试进行更新时,出现以下错误:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Maxpou\BeerBundle\Service\BeerListener::__construct(), 0 passed in /var/www/admin2/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/DefaultEntityListenerResolver.php on line 73 and exactly 1 expected in /var/www/admin2/src/Maxpou/BeerBundle/Service/BeerListener.php:12
Stack trace:
#0 /var/www/admin2/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/DefaultEntityListenerResolver.php(73): Maxpou\BeerBundle\Service\BeerListener->__construct()
#1 /var/www/admin2/vendor/doctrine/orm/lib/Doctrine/ORM/Event/ListenersInvoker.php(110): Doctrine\ORM\Mapping\DefaultEntityListenerResolver->resolve('Maxpou\\BeerBundle\\...')
#2 /var/www/admin2/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(1075): Doctrine\ORM\Event\ListenersInvoker->invoke(Object(Doctrine\ORM\Mapping\ClassMetadata), 'postUpdate', Object(Maxpou\BeerBundle\Entity\Beer), Object(Doctrine\ORM\Event\LifecycleEventArgs), 3)
#3 /var/www/admin2/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(384): Doctrine\ORM\Unit in /var/www/admin2/src/Maxpou/BeerBundle/Service/BeerListener.php on line 12
看来,我没有在BeerListener中获得 $ cacheDriver 参数
有人会知道为什么?
PS:我使用&#34; symfony / symfony&#34;:&#34; ~2.5.0&#34;,&#34; doctrine / orm&#34;:&# 34; ~2.2&#34;,&#34; doctrine / doctrine-bundle&#34;:&#34; ~1.2&#34;,&#34; doctrine / doctrine-cache-bundle&#34;:&# 34; ~1.0&#34;,&#34; snc / redis-bundle&#34;:&#34; 1.1.x&#34;,&#34; predis / predis&#34;:&#34; ^ 1.0 &#34; (在NGINX / PHP-7.1 env中)