Cache Component
PSR-6/PSR-16 cache with adapters for filesystem, APCu, Redis, Memcached. Used internally and available to your services.
- Inject `CacheInterface` (PSR-16 contract). Use `$cache->get($key, fn($item) => ...)` — runs the callable only on miss.
- Set TTL via `$item->expiresAfter(3600)` inside the callable.
- Tag-based invalidation (TagAwareAdapter) lets you bust groups of entries by tag.
- Pools live under `framework.cache.pools` — name them, scope them to specific adapters, inject by name with `#[Target(\'app.cache\')]`.
- Doctrine\'s second-level cache and result cache use the same adapters.
- Clear with `bin/console cache:pool:clear <pool_name>`.
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
$value = $cache->get('user_'.$id, function (ItemInterface $item) use ($id) {
$item->expiresAfter(3600);
return $this->repo->find($id);
});
Common gotchas
- Cache keys can\'t contain `{}()/\@:`. Use `\InvalidArgumentException`-safe characters; the cache contracts validate this.
- Stale-while-revalidate is opt-in (`CacheInterface::get(... beta: ...)`) — without it, every miss is a thundering-herd risk on hot keys.