Last active
April 16, 2020 08:47
-
-
Save dmaicher/e282c59d81dedde89bacb3bb9bb360cb to your computer and use it in GitHub Desktop.
use symfony/cache 3.2 tag aware cache as Doctrine DBAL query result cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$sql = 'some query'; | |
$parameters = []; | |
$types = []; | |
$queryCacheProfile = new QueryCacheProfile(7200, null, $tagAwareResultCache); | |
// use 'foo' as tag for query cache item | |
$tagAwareResultCache->setQueryCacheTags($sql, $parameters, $types, ['foo']); | |
// perform query and write it into our cache | |
$connection->executeCacheQuery($sql, $parameters, $types, $queryCacheProfile); | |
// invalidate previously stored cache item | |
$tagAwareResultCache->getTagAwareAdapter()->invalidateTags(['foo']); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Doctrine\DBAL\Cache\QueryCacheProfile; | |
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface; | |
use Symfony\Component\Cache\CacheItem; | |
use Symfony\Component\Cache\DoctrineProvider; | |
class TagAwareQueryResultCache extends DoctrineProvider | |
{ | |
/** | |
* @var TagAwareAdapterInterface | |
*/ | |
private $tagAwareAdapter; | |
/** | |
* @var array | |
*/ | |
private $queryTags = []; | |
/** | |
* @var string | |
*/ | |
private $currentIdWithoutNamespace; | |
/** | |
* @param TagAwareAdapterInterface $tagAwareAdapter | |
*/ | |
public function __construct(TagAwareAdapterInterface $tagAwareAdapter) | |
{ | |
parent::__construct($tagAwareAdapter); | |
$this->tagAwareAdapter = $tagAwareAdapter; | |
} | |
/** | |
* @param $query | |
* @param array $params | |
* @param array $types | |
* | |
* @return string | |
*/ | |
private function getDoctrineQueryCacheKey($query, array $params, array $types) | |
{ | |
return (new QueryCacheProfile())->generateCacheKeys($query, $params, $types)[0]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function save($id, $data, $lifeTime = 0) | |
{ | |
$this->currentIdWithoutNamespace = $id; | |
return parent::save($id, $data, $lifeTime); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function doSave($id, $data, $lifeTime = 0) | |
{ | |
/** @var CacheItem $item */ | |
$item = $this->tagAwareAdapter->getItem(rawurlencode($id)); | |
if (isset($this->queryTags[$this->currentIdWithoutNamespace])) { | |
$item->tag($this->queryTags[$this->currentIdWithoutNamespace]); | |
} | |
if (0 < $lifeTime) { | |
$item->expiresAfter($lifeTime); | |
} | |
$this->currentIdWithoutNamespace = null; | |
return $this->tagAwareAdapter->save($item->set($data)); | |
} | |
/** | |
* @param string $query | |
* @param array $params | |
* @param array $types | |
* @param array $tags | |
* | |
* @return $this | |
*/ | |
public function setQueryCacheTags($query, array $params, array $types, array $tags) | |
{ | |
$this->queryTags[$this->getDoctrineQueryCacheKey($query, $params, $types)] = $tags; | |
return $this; | |
} | |
/** | |
* @return TagAwareAdapterInterface | |
*/ | |
public function getTagAwareAdapter() | |
{ | |
return $this->tagAwareAdapter; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@dmaicher but in your case used two separate object
QueryCacheProfile
one inTagAwareQueryResultCache
and one forexecuteCacheQuery
. Perhaps better use one like in my revision