Doctrine Result Cache uses square brackets for the Cache Key which makes it difficult to clear #11020
-
Hey, I am currently trying to implement the Result Cache to a repository function., which looks like that: $query = $qb
->where($expr->eq('user.custom_id', ':customId'))
->setParameter('customId', $someVariable, Types::STRING)
->getQuery()
->enableResultCache(240, 'my-cachekey')
->getOneOrNullResult(); However, I only want to cache the result, when it is not empty. So I never want a null result to be cached. Which is why I came up with the following solution: $query = $qb
->where($expr->eq('user.custom_id', ':customId'))
->setParameter('customId', $someVariable, Types::STRING)
->getQuery();
$result = $query
->enableResultCache(240, 'my-cachekey')
->getOneOrNullResult();
if (!$result) {
$this->_em->getConfiguration()->getResultCache()?->deleteItem('my-cachekey');
$result = $query->getOneOrNullResult();
}
return $result; Well, it is not really a solution, because it doesn't work. After some digging I found out that the cache key doctrine uses for the result cache isn't the exact cache key I have provided:
This is the cache key I have to use to clear the result cache. Why does it behave like this and how can I solve the problem? It feels not right not hardcode the square brackets. 🤔 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I solved it by myself. I should have used the CacheDriver instead of the CachePool, because the Driver uses the namespaces to delete the item. Like this: $this->_em->getConfiguration()->getResultCacheImp()->delete('my-cachekey'); |
Beta Was this translation helpful? Give feedback.
I solved it by myself. I should have used the CacheDriver instead of the CachePool, because the Driver uses the namespaces to delete the item. Like this: