doctrine/doctrine-cache-bundle
Installation (if still needed for legacy projects):
composer require doctrine/doctrine-cache-bundle
Note: For new projects, skip this and use Symfony’s native symfony/cache instead.
Configuration (if using legacy bundle):
Add to config/packages/doctrine_cache.yaml:
doctrine_cache:
providers:
my_provider:
namespace: my_app.cache
type: file_system
directory: '%kernel.cache_dir%/doctrine'
First Use Case: Inject the cache provider into a service:
use Doctrine\Common\Cache\CacheProvider;
class MyService
{
private $cache;
public function __construct(CacheProvider $cache)
{
$this->cache = $cache;
}
public function fetchData()
{
$key = 'my_key';
if (!$this->cache->contains($key)) {
$this->cache->save($key, $this->generateExpensiveData());
}
return $this->cache->fetch($key);
}
}
Cache Layer for Doctrine Queries:
Use Doctrine\Common\Cache\CacheProvider to cache DQL queries:
$cache = $this->cacheProvider->getItem('query_result');
if (!$cache->isHit()) {
$query = $entityManager->createQuery('SELECT u FROM User u');
$cache->set($query->getResult());
}
return $cache->get();
Metadata Cache:
Configure metadata_cache in config/packages/doctrine.yaml:
doctrine:
orm:
metadata_cache_driver: doctrine_cache
Query Cache:
Enable query caching in config/packages/doctrine.yaml:
doctrine:
orm:
query_cache_driver: doctrine_cache
Symfony Cache Adapter:
Bridge Doctrine Cache with Symfony’s CacheInterface:
use Symfony\Component\Cache\Adapter\DoctrineProviderAdapter;
$doctrineCache = $this->cacheProvider;
$symfonyCache = new DoctrineProviderAdapter($doctrineCache);
Autowiring:
Define custom cache providers in services.yaml:
services:
App\Cache\MyCacheProvider:
arguments:
$provider: '@doctrine.cache.provider.my_provider'
Environment-Specific Config:
Use %env% for cache directory:
doctrine_cache:
providers:
my_provider:
directory: '%env(CACHE_DIR)%/doctrine'
Deprecation Warning:
symfony/cache or manual Doctrine Cache setup.# Old (deprecated)
doctrine_cache: ~
# New (recommended)
framework:
cache:
app: cache.adapter.doctrine
services:
cache.adapter.doctrine:
class: Symfony\Component\Cache\Adapter\DoctrineProviderAdapter
arguments:
- '@doctrine.cache.provider.default'
Namespace Collisions:
namespace in doctrine_cache.yaml is unique (e.g., app.cache).File Permissions:
file_system provider, ensure %kernel.cache_dir% is writable:
chmod -R 775 %kernel.cache_dir%
Serialization Issues:
__serializeMagicMethods() or implement Serializable.Cache Misses:
Enable debug logging for Doctrine\Common\Cache:
monolog:
handlers:
main:
level: debug
channels: ['doctrine']
Clear Cache: Clear Doctrine Cache manually:
$this->cacheProvider->deleteAll();
Custom Providers:
Extend Doctrine\Common\Cache\CacheProvider for specialized storage (e.g., Redis):
use Doctrine\Common\Cache\RedisCache;
class RedisProvider extends RedisCache
{
public function __construct(\Redis $client, array $options = [])
{
parent::__construct($client, $options);
}
}
Event Listeners:
Listen to cache.clear events (Symfony 4+):
use Symfony\Component\Cache\CacheEvents;
$cache->on(CacheEvents::CLEAR, function () {
// Custom logic on cache clear
});
Tag-Based Invalidation:
Use Doctrine\Common\Cache\CacheProvider::delete() with tags (if supported by the provider):
$this->cacheProvider->delete('tag:user_123');
How can I help you explore Laravel packages today?