liip/doctrine-cache-bundle
Deprecated Symfony2 bundle integrating Doctrine Common Cache. Defines cache services via config with namespaces, types (APC, filesystem, memcached), directories/hosts/ports, and aliases. Use doctrine/doctrine-cache-bundle instead.
Installation:
composer require liip/doctrine-cache-bundle:~1.0
Register the bundle in config/bundles.php:
return [
// ...
Liip\DoctrineCacheBundle\LiipDoctrineCacheBundle::class => ['all' => true],
];
Basic Configuration (config/packages/liip_doctrine_cache.yaml):
liip_doctrine_cache:
namespaces:
default:
type: apc
First Use Case: Inject the cache service into a controller/service:
use Doctrine\Common\Cache\Cache;
class MyService
{
public function __construct(private Cache $cache)
{
}
public function fetchData()
{
$data = $this->cache->fetch('my_key');
if ($data === null) {
$data = $this->generateData();
$this->cache->save('my_key', $data, 3600); // Cache for 1 hour
}
return $data;
}
}
Multi-Cache Namespaces:
Use distinct namespaces for different data types (e.g., users, products):
liip_doctrine_cache:
namespaces:
users:
type: memcached
host: memcached
products:
type: file_system
directory: "%kernel.cache_dir%/products"
Dependency Injection:
Define services with cache dependencies in config/services.yaml:
services:
App\Service\ProductService:
arguments:
$cache: '@liip_doctrine_cache.ns.products'
Doctrine Integration:
Configure Doctrine to use the cache for queries (config/packages/doctrine.yaml):
doctrine:
orm:
metadata_cache_driver: liip_doctrine_cache.ns.metadata
query_cache_driver: liip_doctrine_cache.ns.queries
result_cache_driver: liip_doctrine_cache.ns.results
Environment-Specific Caching:
Override cache types in config/packages/dev/liip_doctrine_cache.yaml:
liip_doctrine_cache:
namespaces:
default:
type: array # Resets per request in dev
symfony/cache for modern alternatives (e.g., Psr6Cache).$cache->save('critical_data', $data, 86400); // Cache for 1 day
$ttl = $isVolatile ? 60 : 86400; // 1 min or 1 day
$cache->save('key', $data, $ttl);
Deprecation Warning:
doctrine/doctrine-cache-bundle.APCu Limitations:
file_system or memcached for shared caching.Memcached Configuration:
id service (e.g., my_memcached_service) is properly configured in Symfony’s DI container.port/host may not match your Memcached setup.File System Permissions:
directory path is writable by the web server user (e.g., www-data):
chmod -R 775 /tmp/lala
Namespace Collisions:
default for both APCu and Memcached).Check Cache Hits/Misses: Implement a wrapper to log cache operations:
$cache->save('key', $data);
// Log: "Cache miss for 'key' (saved)"
Clear Caches:
Manually delete files in file_system caches or flush APCu:
apcu_clear_cache() # Requires APCu extension
Validate Configuration:
Use Symfony’s debug toolbar or bin/console debug:container to verify cache services are registered.
Custom Cache Drivers:
Create a service implementing Doctrine\Common\Cache\Cache:
services:
app.custom_cache:
class: App\Cache\RedisCache
arguments:
- '@redis.client'
tags: ['liip_doctrine_cache.type', { name: 'redis' }]
Then use it in config:
liip_doctrine_cache:
namespaces:
redis_cache:
type: redis
Cache Events: Listen for cache saves/misses via Symfony events (if using a wrapper class).
Fallback Strategies:
Combine caches (e.g., array as fallback for file_system):
if (($data = $primaryCache->fetch('key')) === null) {
$data = $fallbackCache->fetch('key');
}
How can I help you explore Laravel packages today?