Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony3:如何以正确的方式启用PDO/Doctrine缓存适配器?_Php_Symfony_Caching_Pdo_Symfony 3.4 - Fatal编程技术网

Php Symfony3:如何以正确的方式启用PDO/Doctrine缓存适配器?

Php Symfony3:如何以正确的方式启用PDO/Doctrine缓存适配器?,php,symfony,caching,pdo,symfony-3.4,Php,Symfony,Caching,Pdo,Symfony 3.4,哎 我正在尝试让缓存适配器在Symfony 3.4中运行。我在这个项目中使用了条令,所以使用这个适配器似乎是可以的(我有两个容器运行这个服务,所以我需要一个缓存系统,这两个容器可以访问…,并且没有Redis/Memcache…,所以请不要对此提建议;) 以下是我所做配置的相关部分: services: cache.adapter.pdo: class: Symfony\Component\Cache\Adapter\PdoAdapter argume

我正在尝试让缓存适配器在Symfony 3.4中运行。我在这个项目中使用了条令,所以使用这个适配器似乎是可以的(我有两个容器运行这个服务,所以我需要一个缓存系统,这两个容器可以访问…,并且没有Redis/Memcache…,所以请不要对此提建议;)

以下是我所做配置的相关部分:

 services:
      cache.adapter.pdo:
        class: Symfony\Component\Cache\Adapter\PdoAdapter
        arguments: [ '@doctrine.dbal.default_connection' ]
      blahfu.using.cache:
        class: App\HeavyCacheUser
        arguments: [ '@app.cache' ]
    

framework:
  cache:
    app: cache.adapter.pdo
    
doctrine:
  dbal:
    default_connection: default
    connections:
      default:...
      ...
我还添加了一个迁移来添加相应的表: 使用以下查询(如
src/Symfony/Component/Cache/Traits/PdoTrait.php
中所示):

当我尝试使用它时,我只得到缓存未命中

例如:

$cacheKey = 'blahfu.bar';

$item = $this->cache->getItem($cacheKey);

if (!$item->isHit()) {
  // Do $stuff
  
  $item->set($stuff)->expiresAfter(900);
  $this->cache->save($item);
  Logger::info('Cache miss');
} else {
  Logger::info('Cache hit');
}

return $item->get();
我错过了什么


谢谢你的帮助:)

我找到了如何让它工作的方法。经过一天的调试,在symfony 3.4中,使用应用程序的服务似乎得到了一个名称空间,而不是给定的连接,因为在symfony的DI组件中,没有实现使用PDOAdapter

我现在所做的是将PDOAdapter服务直接注入到需要的服务中:

services:
      cache.adapter.pdo:
        class: Symfony\Component\Cache\Adapter\PdoAdapter
        arguments: [ '@doctrine.dbal.default_connection' ]
      blahfu.using.cache:
        class: App\HeavyCacheUser
        arguments: [ '@cache.adapter.pdo' ]
然后当然注射它(我以前也注射过,但没有提到):


我发现,我得到的是像“SyybYKXlnh”这样的字符串,而不是连接对象。。。又是一件我不明白的事。
services:
      cache.adapter.pdo:
        class: Symfony\Component\Cache\Adapter\PdoAdapter
        arguments: [ '@doctrine.dbal.default_connection' ]
      blahfu.using.cache:
        class: App\HeavyCacheUser
        arguments: [ '@cache.adapter.pdo' ]
use Symfony\Component\Cache\Adapter\AdapterInterface;

class HeavyCacheUser
  private $cache;
  public function __construct(AdapterInterface $cacheAdapter){
    $this->cache = $cache;
  }