Installation Add the bundle via Composer:
composer require boson/cache-bundle
Enable the bundle in config/bundles.php:
Boson\CacheBundle\BosonCacheBundle::class => ['all' => true],
Basic Configuration
Configure the cache driver in config/packages/boson_cache.yaml:
boson_cache:
driver: 'cache.app' # Default Symfony cache driver
prefix: 'boson_' # Optional prefix for keys
First Use Case Inject the cache service into a controller or service:
use Boson\CacheBundle\Service\CacheService;
class MyController extends AbstractController
{
public function __construct(private CacheService $cache)
{
}
public function index()
{
$this->cache->set('key', 'value', 3600); // Cache for 1 hour
$value = $this->cache->get('key');
return new Response($value);
}
}
Caching API Responses
$response = $this->cache->remember('api_response', 300, function () {
return $this->httpClient->request('GET', 'https://api.example.com/data');
});
Tag-Based Invalidation
# config/packages/boson_cache.yaml
boson_cache:
tags: ['users', 'products']
$this->cache->set('user_123', $user, 3600, ['users']);
$this->cache->clearTags(['users']); // Invalidate all 'users' tagged items
Integration with Doctrine
$this->cache->getDoctrineCache()->get('entity_manager', function () {
return $this->entityManager;
});
users.{id}) to avoid collisions.remember() for expensive operations with fallback logic.Driver Mismatch
cache.app is misconfigured, the bundle silently falls back to array driver.framework.cache in config/packages/framework.yaml.Tag Invalidation Race Conditions
clearTags() sparingly in critical paths.Prefix Collisions
boson_) may conflict with other bundles.boson_cache:
prefix: 'myapp_boson_'
Enable Cache Debugging
# config/packages/boson_cache.yaml
boson_cache:
debug: true
Logs cache hits/misses to var/log/dev.log.
Clear Cache Programmatically
$this->cache->clear(); // Clears all Boson cache items
Custom Cache Item Pool Override the default pool in a service:
# config/services.yaml
services:
Boson\CacheBundle\Service\CacheService:
arguments:
$pool: '@my_custom.cache_pool'
Event Listeners
Subscribe to cache events (e.g., CacheItemPoolClearEvent):
use Boson\CacheBundle\Event\CacheEvents;
$dispatcher->addListener(CacheEvents::CLEAR_TAG, function ($event) {
// Custom logic on tag invalidation
});
PSR-6 Compliance
The bundle uses Symfony’s CacheItemPoolInterface. Extend it for custom storage backends (e.g., Redis, Memcached).
How can I help you explore Laravel packages today?