sonata-project/cache-bundle
Symfony bundle providing caching services for Sonata projects, with pluggable cache backends and integration helpers. Note: this repository is abandoned and no longer actively maintained; community help welcome.
Installation:
composer require sonata-project/cache-bundle
Add to config/bundles.php:
return [
// ...
Sonata\CacheBundle\SonataCacheBundle::class => ['all' => true],
];
Basic Configuration:
Configure cache adapters in config/packages/sonata_cache.yaml:
sonata_cache:
app:
driver: cache.app
namespace: sonata.cache
default_lifetime: 3600
cache:
driver: cache.array
First Use Case:
Inject CacheManagerInterface into a service/controller:
use Sonata\Cache\CacheManagerInterface;
class MyController extends AbstractController
{
public function __construct(private CacheManagerInterface $cacheManager) {}
public function index()
{
$cache = $this->cacheManager->getCache('app');
$data = $cache->get('my_key') ?? 'default_value';
return $this->json($data);
}
}
Cache Retrieval/Storage:
// Store
$cache->set('key', $value, 3600); // 1-hour TTL
// Retrieve
$value = $cache->get('key');
// Delete
$cache->delete('key');
Tag-Based Invalidation:
# config/packages/sonata_cache.yaml
sonata_cache:
app:
driver: cache.app
tags: ['my_tag']
$cache->invalidateTags(['my_tag']); // Invalidate all keys with this tag
Doctrine Integration:
# config/packages/sonata_cache.yaml
sonata_cache:
doctrine:
metadata_cache_driver: cache.app
query_cache_driver: cache.app
result_cache_driver: cache.app
SSI/ESI Caching:
{# ESI Example #}
{% cache 'esi_key' %}
{% render url('path/to/controller') %}
{% endcache %}
cache:clear commands via CLI:
php bin/console sonata:cache:flush-all
CacheManager for multi-tenant apps:
class TenantCacheManager extends CacheManager
{
public function getCache(string $name, string $tenantId): CacheInterface
{
return $this->getCache($tenantId . '.' . $name);
}
}
use Sonata\CacheBundle\Event\CacheEvent;
class MyCacheListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
CacheEvent::INVALIDATE => 'onCacheInvalidate',
];
}
public function onCacheInvalidate(CacheEvent $event): void
{
// Handle invalidation logic
}
}
Deprecated Classes:
Sonata\CacheBundle\* classes; use Sonata\Cache\* instead (e.g., CacheManager → Sonata\Cache\CacheManager).config/services.yaml.Command Registration:
sonata:cache:flush may fail if not properly autowired.Sonata\CacheBundle\Command\* classes are loaded via autowiring or explicit service definitions.PHP/Symfony Version Mismatch:
Twig Integration:
sonata_cache:
twig:
enabled: true
Cache Invalidation Race Conditions:
invalidateTags() in a transactional context or add retry logic.php bin/console debug:cache
CacheEvent::MISS/CacheEvent::HIT events.Custom Cache Drivers:
Implement Sonata\Cache\CacheInterface and register via:
services:
my.custom_cache:
class: App\Cache\MyCustomCache
tags: ['sonata.cache.driver']
Override Default Services:
# config/services.yaml
Sonata\Cache\CacheManager: '@app.custom_cache_manager'
Extend CacheManager:
class CustomCacheManager extends CacheManager
{
public function getCache(string $name): CacheInterface
{
if ($name === 'special') {
return $this->getCache('app'); // Fallback logic
}
return parent::getCache($name);
}
}
namespace in sonata_cache.yaml is unique per environment (e.g., dev.sonata.cache).sonata_cache.yaml:
sonata_cache:
redis:
driver: predis
host: 127.0.0.1
password: 'your_password'
How can I help you explore Laravel packages today?