Using Symfony 7, I have defined my custom cache pool:
framework:
cache:
default_redis_provider: '%env(resolve:REDIS_URL)%'
pools:
cache.score:
adapter: cache.adapter.redis_tag_aware
According to symfony docs the proper way to autowire is using CacheInterface $cacheScore. While this does autowire the RedisTagAwareAdapter in my case, strictly speaking it restricts me to the CacheInterface methods (at least in my IDE, but also there is no guarantee that the object I get is going to implement anything else than the CacheInterface).
The problem is that this interface implements limited methods, it lacks the AdapterInterface::getItem() function. To be clear, every adapter implements both CacheInterface (which provides get method using cache contracts) and AdapterInterface which is based on the native PHP's Psr\Cache\CacheItemPoolInterface and provides getItem.
In my use-case I need to use both of these at the same time. I did not find which class I should autowire for this to work. I tried AbstractAdapter - this fails
Symfony\Component\DependencyInjection\Exception\RuntimeException : Cannot autowire service "App\ScorePredictor\DuelScorePredictor": argument "$cacheScore" of method "__construct()" references class "Symfony\Component\Cache\Adapter\AbstractAdapter" but no such service exists. Try changing the type-hint to one of its parents: interface "Symfony\Contracts\Cache\CacheInterface", or interface "Psr\Cache\CacheItemPoolInterface".
The problem as I explained is that each of the interfaces provide only half of the functionality I need.
I cannot even autowire explicitly the RedisTagAwareAdapter type, because the same error is thrown.
I need a solution that will provide me with flexibility, so that if I would like to change Redis adapter, I can do so only in cache.yaml and no code modification would be needed.
P.S. The symfony console debug:autowiring cache does not help either:
Autowirable Types
=================
The following classes & interfaces can be used as type-hints when autowiring:
(only showing classes/interfaces matching cache)
CacheItemPoolInterface generates CacheItemInterface objects.
Psr\Cache\CacheItemPoolInterface - alias:cache.app
Psr\Cache\CacheItemPoolInterface $cacheScore - target:cache.score
Covers most simple to advanced caching needs.
Symfony\Contracts\Cache\CacheInterface - alias:cache.app
Symfony\Contracts\Cache\CacheInterface $cacheScore - target:cache.score
Allows invalidating cached items using tags.
Symfony\Contracts\Cache\TagAwareCacheInterface - alias:cache.app.taggable
Symfony\Contracts\Cache\TagAwareCacheInterface $cacheScore - target:cache.score