【问题标题】:How to detect if Doctrine query is from cache?如何检测 Doctrine 查询是否来自缓存?
【发布时间】:2017-05-26 15:08:04
【问题描述】:

我正在尝试将 Doctrine Result Caching 与 Redis 和 Predis 以及 SncRedisBundle 和 Symfony 一起使用。

我想知道我的cache+doctrine+redis+predis+SncRedisBundle配置是否设置正确

snc_redis:
    clients:
        default:
            type: predis
            alias: default
            dsn: redis://localhost
    doctrine:
        metadata_cache:
            client: default
            entity_manager: default          
            document_manager: default        
        result_cache:
            client: default
            entity_manager: [default, read]  
        query_cache:
            client: default
            entity_manager: default

因为下面的查询显示了预期的内容,但我怎么知道它是否来自缓存?

$query = $repository->createQueryBuilder('p')
    ->where('p.id < :id')
    ->setParameter('id', '100')
    ->orderBy('p.id', 'ASC')
    ->getQuery()
    ->useQueryCache(true)    
    ->useResultCache(true); 

$products = $query->getResult();

【问题讨论】:

    标签: symfony doctrine predis


    【解决方案1】:

    正如您在此测试中看到的 https://github.com/doctrine/doctrine2/blob/master/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php#L40 没有执行此操作的函数。

    但是,您可以通过定义自己的 cacheId 或使用 -&gt;getQuery()-&gt;getResultCacheId()deprecated -&gt;getQuery()-&gt;getQueryCacheProfile()-&gt;getCacheKey()-&gt;getQuery()-&gt;getResultCacheDriver()-&gt;fetch($cacheKey) 来实现此目的

    来源:https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/AbstractQuery.php#L945

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-14
      • 2011-05-06
      • 2020-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多