清除Symfony2/Doctrine中的查询缓存

Ada*_*dam 7 caching doctrine symfony

我最近编写了我的第一个Symfony2应用程序,一切都很好,除了现在我想添加一些查询缓存来提高性能并减少不必要的查询.我已将以下行添加到一个特定查询的构建器中:

$query->useResultCache(true)
      ->useQueryCache(true);
Run Code Online (Sandbox Code Playgroud)

在第一次请求之后,然后按预期使用缓存.我可以在分析器中验证.一切都很棒!

问题是我还有一个简单的管理面板,我写了允许用户修改内容,但更改后仍然使用缓存版本.

有没有一种方法可以"编程"告诉Symfony2/Doctrine在更新数据时清除查询缓存,还是有办法配置它?

这似乎是一个常见的问题,但我在Google上找不到与此问题有关的任何内容!

tom*_*rke 25

我建议使用结果缓存ID - 这样您就可以清除一个特定的结果缓存:

$query->setResultCacheId('my_custom_id');
// or shorter notation with lifetime option
$query->useResultCache(true, 3600, 'my_custom_id');

// to delete cache
$cacheDriver = $entityManager->getConfiguration()->getResultCacheImpl();
$cacheDriver->delete('my_custom_id');
// to delete all cache entries
$cacheDriver->deleteAll();
Run Code Online (Sandbox Code Playgroud)

有关缓存删除的更多信息,请参阅:http:
//docs.doctrine-project.org/en/latest/reference/caching.html#deleting