Use Symfony app cache as a service

Thomas P
1 min readAug 3, 2017

--

I you wan’t to use Symfony app cache (with redis for example), this is a simple how-to:

# config_prod.yml
framework:
cache:
app: cache.adapter.redis
default_redis_provider: "redis://%redis_host%"

Then use it as a service with `cache.app` name.

When injected in your service, you will retrieve an Symfony\Component\Cache\Adapter\TraceableAdapter. But when available, best practise is to typint it as the PSR’s interface.

public function __construct(
Psr\Cache\CacheItemPoolInterface $cacheItemPool
)

Then all the methods defined by the PSR are available.
http://www.php-fig.org/psr/psr-16/#interfaces

--

--

Thomas P
Thomas P

Written by Thomas P

Symfony lover, Gopher and opensource enthusiast. Ex-firefighter 🚒, I miss cuting out cars 🚙.

Responses (1)