-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Description
Symfony version(s) affected: 4.4.1 (probably >3.2)
Description
I'm using a tagAware cache, like so:
framework:
cache:
pools:
app.cache_pool:
adapters:
- cache.adapter.array
- cache.adapter.apcu
- cache.adapter.redis
tags: true
but when using it (in env=dev), I get an error:
Attempted to call an undefined method named "invalidateTags" of class "Symfony\Component\Cache\Adapter\TraceableAdapter".
How to reproduce
Add config (see above)
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
class TestController
{
/** @var TagAwareAdapterInterface */
private $appCachePool;
// note: automatic Dependency Injection is tied to this interface.. but for PHPStorm we typehint it TagAwareAdapterInterface, because the actual adapter is expected to be TagAware (because of config `tags: true`)
public function __construct(CacheItemPoolInterface $appCachePool)
{
$this->cache = $appCachePool;
}
/** @Route("/test") */
public function test()
{
$this->cache->invalidateTags('sometag');
}
}
Possible Solution
Looks easy enough to add the TagAwareAdapterInterface
to the TraceableAdapter
and implement some logic to trace the usage of tags. A separate story could be made to use this info in the profiler to show information about tags being used and invalidated.
Adding the code snippet below was enough to solve the bug. The actual implementation (for the profiler) could be a separate feature request.
class TraceableAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface, TagAwareAdapterInterface
...
public function invalidateTags(array $tags)
{
// TODO: Implement invalidateTags() method.
}
}
Additional context
Possibly related to #20764.
The solution from @pascalwacker in 2017 probably works.. but I think it's an ugly work-around. I personally think we should support the automatic autowire and also make tagAware cache traceable and show it in the profiler at some point (separate feature request probably).
Also note that tag()
is always available in the ItemInterface
, which is the default way to use the cache, as used through the cache contracts. This means it's a part of the Symfony eco-system and should be part of it's profiler and autowiring