【问题标题】:Symfony 4 + Doctrine DBAL cache driverSymfony 4 + Doctrine DBAL 缓存驱动
【发布时间】:2018-12-18 05:51:37
【问题描述】:

我已经开始了一个非常简单的项目,只有 symfony/skeleton 和教义/doctrine-bundle。

我没有使用 ORM,只是简单的 DBAL。没有安装 symfony/orm-pack。

我正在尝试缓存一个简单查询的结果,但出现异常消息“尝试缓存查询但未配置结果驱动程序”。

示例代码:

$query = $connection->createQueryBuilder()
    ->select('*')
    ->from('common.Language')
    ->getSQL();
$qcp = new QueryCacheProfile(0, "languages");
$stmt = $connection->executeCacheQuery($query, [], [], $qcp);
$rows = $stmt->fetchAll();
$stmt->closeCursor();

我读过我可以通过在容器编译阶段 here 实现一个小包来修改 DBAL 连接配置来启用缓存。 但我无法让它工作。我猜是因为它试图将 ORM 正在使用的相同缓存驱动程序分配给 DBAL,但我没有安装 ORM。

我该怎么做?我创建了一个简单的类,它注入 DBAL 连接并在运行时配置缓存,然后我所有的存储库/daos 注入该类。但我不认为这是实现这一目标的正确方法......

public function __construct(Connection $connection, ContainerInterface $container)
{
    /** @var Kernel $kernel */
    $kernel = $container->get('kernel');

    $cacheProviders = [];

    if ($kernel->isDebug()) {
        $cacheProviders[] = new ArrayCache();
    } else {
        $cacheProviders[] = new PhpFileCache($kernel->getCacheDir().'/dbal');
    }

    $connection->getConfiguration()->setResultCacheImpl(new ChainCache($cacheProviders));
}

PD:使用 symfony 4.1 和 DBAL 2.7.1

【问题讨论】:

    标签: symfony caching dbal


    【解决方案1】:

    根据你的帖子https://github.com/doctrine/DoctrineBundle/issues/833

    并引用仍然打开的 https://github.com/doctrine/DoctrineBundle/issues/114 所以这个功能没有解决!

    对于临时解决问题,我建议您将executeCacheQuery 替换为executeQuery +test:

        $qcp = new QueryCacheProfile('3600', "someKey");
        $qcp = empty($qcp->getResultCacheDriver()) ? null : $qcp;//test for existing 
    
    
        $conn = $this->getEntityManager()->getConnection();
        $stmt = $conn->executeQuery(
            $query,
            [],
            [],
            $qcp
    

    阅读DOCs 后,注意到配置未设置已更新:

        $conn = $this->getEntityManager()->getConnection();
        $cache = new \Doctrine\Common\Cache\ArrayCache();
        $config = $conn->getConfiguration();
        $config->setResultCacheImpl($cache);
    
        $qcp = new QueryCacheProfile('3600', "someKey", $cache);
        $qcp = empty($qcp->getResultCacheDriver()) ? null : $qcp;//cache driver exist?
    
        $stmt = $conn->executeQuery(
            $query,
            [],
            [],
            $qcp
        );
    

    【讨论】:

      猜你喜欢
      • 2021-12-03
      • 2017-06-13
      • 1970-01-01
      • 2018-05-26
      • 2012-01-19
      • 2016-04-16
      • 2021-12-07
      • 2015-10-14
      • 2012-11-07
      相关资源
      最近更新 更多