Description
Symfony version(s) affected: 4.3.2
Description
When using TagAwareAdapter, the tags attached to an item are not saved in cache, even if the actual items are saved.
How to reproduce
I'm using the following structure:
framework:
cache:
app: cache.adapter.redis
default_redis_provider: "redis://%env(REDIS_CACHE_URL)%"
pools:
app.cache.redis.tags:
adapter: cache.adapter.redis
provider: cache.default_redis_provider
app.cache.redis.items:
adapter: cache.adapter.redis
provider: cache.default_redis_provider
app.cache.redis.simple:
adapter: cache.adapter.redis
provider: cache.default_redis_provider
services:
app.cache.redis.taggable:
class: 'Symfony\Component\Cache\Adapter\TagAwareAdapter'
arguments:
- '@app.cache.redis.items'
- '@app.cache.redis.tags'
MyAwesomeTaggableRedisCache:
arguments:
$adapter: '@app.cache.redis.taggable'
and cache the items with tags attached:
//$this->adapter is app.cache.redis.taggable
$key = 'some_key';
$cachedItem = $this->adapter->getItem($key);
$cachedItem->expiresAfter(600);
$tags=['tag1', 'tag2'];
$cachedItem->tag($tags);
if (false === $cachedItem->isHit()) {
$result = call_user_func_array($function, $parameters);//get data from db
$cachedItem->set(serialize($result));
$this->adapter->save($cachedItem);
}
$result = unserialize($cachedItem->get());
return $result;
Items are correctly cached, and retrieved from cache. However, the tags (in this scenario tag1, tag2) don't seem to be cached, and it's always a miss.
If I invalidate by a given tag (eg tag1), then that tag is cached (the items attached to the cached are invalidate a well, so no issue here).
$this->adapter->invalidateTags(['tag1']);
From what I saw in the TagAwareAdapter class, this behaviour seems intended (only save tag in cache when invalidating by tag).
My question is: why not cache the tag also when the first item with the given tag is saved?
Possible Solution
Save tag when the first item with the given tag is persisted.