-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Description
Hi,
we use in Shopware 6 the TagAwareAdapterInterface
.
We also use this adapter for the HttpCache by collection all generated tags during the request and add them to the HttpCache cache item.
The collection of these tags is done by decorating the Cache Adapter and each time a cache item is read or written, we add the tags of the items to a global Collection.
The problem is that, when a CacheItem is written, the new tags are under CacheItem::newMetaData
and we have no access to this value from the decoration.
Of course there are several ways to get around this (implementing your own CacheItem, etc.), but this would of course cause a lot of overhead. Currently we solve this by reading the cache item directly after the save.
It would help us a lot if you would provide a getter for newMetaData
or if you would not declare the CacheItem
class as final - so I could implement a static accessor.
Example
Here code of the decorated cache adapter:
class CacheDecorator implements TagAwareAdapterInterface
{
/** @var TagAwareAdapterInterface */
private $decorated;
/** @var CacheTagCollection */
private $collection;
public function __construct(TagAwareAdapterInterface $decorated, CacheTagCollection $collection)
{
$this->decorated = $decorated;
$this->collection = $collection;
}
public function getItem($key)
{
$item = $this->decorated->getItem($key);
$this->collection->add($this->getTags($item));
return $item;
}
public function save(CacheItemInterface $item)
{
$result = $this->decorated->save($item);
// this is a problem, we do not have access to the $newMetaData
$item = $this->decorated->getItem($item->getKey());
$this->collection->add($this->getTags($item));
return $result;
}
private function getTags(CacheItemInterface $item): array
{
if (!$item instanceof CacheItem) {
return [];
}
$metaData = $item->getMetadata();
return $metaData['tags'] ?? [];
}
}