Installation:
composer require cache/adapter-bundle
Add to config/bundles.php (Symfony):
return [
// ...
Cache\AdapterBundle\CacheAdapterBundle::class => ['all' => true],
];
Basic Configuration:
Edit config/packages/cache_adapter.yaml (Symfony) or define in config/services.php (Laravel via Symfony integration):
cache:
app: '%env(CACHE_DRIVER)%' # e.g., 'redis', 'apcu', 'filesystem'
default_lifetime: 3600
pools:
redis:
adapter: cache.redis
provider: 'redis://localhost'
filesystem:
adapter: cache.filesystem
provider: '%kernel.cache_dir%/cache'
First Use Case: Inject the cache pool in a service/controller:
use Psr\Cache\CacheItemPoolInterface;
class MyService
{
public function __construct(private CacheItemPoolInterface $cache)
{
}
public function getCachedData(string $key): ?string
{
return $this->cache->getItem($key)->get();
}
}
Tag-Based Invalidation:
// Set with tags
$item = $this->cache->getItem('user:123');
$item->set('John Doe');
$item->setTags(['users', 'profile']);
$this->cache->save($item);
// Clear by tag
$this->cache->deleteItemsMatchingTag('users');
Hierarchical Caching:
Configure in config/packages/cache_adapter.yaml:
cache:
pools:
hierarchy:
adapter: cache.hierarchy
provider:
layers:
- cache.redis
- cache.filesystem
Laravel Integration (via Symfony Bridge):
symfony/cache in Laravel (if installed):
$cache = app('cache.app'); // Uses Symfony's CacheItemPoolInterface
$this->app->bind(CacheItemPoolInterface::class, function ($app) {
return $app->make('cache.app');
});
Dynamic Pool Selection:
// Resolve a specific pool
$redisPool = $this->container->get('cache.redis');
getItem() + save() over set()/get() for consistency with PSR-6.user:profile) to avoid collisions.Deprecated Package:
symfony/cache) if active maintenance is needed.Configuration Overrides:
cache_adapter.yaml may conflict with Laravel’s cache.php. Explicitly define pools to avoid ambiguity.Tagging Limitations:
cache.hierarchy with a tag-aware layer (e.g., Redis).Lifetime Handling:
default_lifetime in config is seconds, not minutes. Set 0 for no expiration.bin/console debug:container | grep cache
cache:
pools:
redis:
adapter: cache.redis
provider: 'redis://localhost'
debug: true # Enable logging
Custom Adapters:
Extend Cache\AdapterBundle\DependencyInjection\CacheExtension to add new adapters (e.g., Memcached).
Event Listeners:
Subscribe to CacheItemPoolInterface events (e.g., CacheItemPool::itemMissed) via Symfony’s event dispatcher.
Laravel-Specific: Override the service provider to integrate with Laravel’s cache manager:
$this->app->extend('cache', function ($app, $value) {
return $app->make('cache.app'); // Map Laravel's cache to PSR-6
});
cache.filesystem with serializer: 'jms_serializer' for complex data.provider: 'redis://cluster:6379' and ensure the adapter supports it.How can I help you explore Laravel packages today?