Skip to content

Getter for \Symfony\Component\Cache\CacheItem::$newMetadata #36697

@OliverSkroblin

Description

@OliverSkroblin

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'] ?? [];
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions