atheon/doctrine-cache-bundle
Installation Add the bundle via Composer (though note the deprecation warning):
composer require atheon/doctrine-cache-bundle
Register the bundle in config/bundles.php:
return [
// ...
Atheon\DoctrineCacheBundle\AtheonDoctrineCacheBundle::class => ['all' => true],
];
Configuration
Configure Doctrine Cache in config/packages/doctrine_cache.yaml (or manually in config/packages/doctrine.yaml):
doctrine_cache:
providers:
my_provider:
namespace: my_namespace
driver: file_system
options:
directory: '%kernel.cache_dir%/doctrine'
First Use Case Use the cache provider in a Doctrine repository or service:
use Doctrine\Common\Cache\CacheProvider;
class MyRepository extends ServiceEntityRepository
{
public function __construct(
private CacheProvider $cacheProvider
) {}
public function getCachedData($key)
{
return $this->cacheProvider->fetch($key);
}
}
Query Result Caching Cache DQL query results in repositories:
$cacheKey = 'query_results_' . md5($query);
$results = $this->cacheProvider->fetch($cacheKey);
if (!$results) {
$results = $this->createQueryBuilder($query)->getResult();
$this->cacheProvider->save($cacheKey, $results, 3600); // Cache for 1 hour
}
Entity Metadata Caching Leverage Doctrine’s built-in cache for metadata:
# config/packages/doctrine.yaml
doctrine:
orm:
metadata_cache_driver: my_provider
query_cache_driver: my_provider
result_cache_driver: my_provider
Service-Layer Caching
Inject CacheProvider into services for business logic caching:
public function __construct(
private CacheProvider $cache,
private MyRepository $repo
) {}
public function getExpensiveData($id)
{
$key = 'expensive_data_' . $id;
return $this->cache->fetch($key) ?? $this->repo->fetchExpensiveData($id);
}
Symfony Cache Integration
Bridge Doctrine Cache with Symfony’s CacheInterface for unified caching:
use Doctrine\Common\Cache\CacheProvider;
use Symfony\Component\Cache\Adapter\AdapterInterface;
class DoctrineCacheAdapter implements AdapterInterface
{
public function __construct(private CacheProvider $provider) {}
public function get($key, $default = null)
{
return $this->provider->fetch($key) ?? $default;
}
public function set($key, $value, $ttl = null)
{
$this->provider->save($key, $value, $ttl);
}
}
Event Listeners Invalidate cache on entity updates:
use Doctrine\ORM\Event\LifecycleEventArgs;
class CacheInvalidator
{
public function postUpdate(LifecycleEventArgs $args)
{
$entity = $args->getObject();
$this->cache->delete("entity_{$entity->getId()}");
}
}
Deprecation Warning
Cache component.# config/packages/doctrine.yaml
doctrine:
orm:
metadata_cache_driver: cache.app
query_cache_driver: cache.app
result_cache_driver: cache.app
# config/packages/cache.yaml
framework:
cache:
app: cache.adapter.doctrine
pools:
doctrine:
adapter: cache.adapter.apcu
Driver Compatibility
file_system, apcu, redis) is installed and configured.doctrine_cache:
providers:
redis_provider:
namespace: redis_ns
driver: predis
options:
host: localhost
port: 6379
Namespace Collisions
doctrine_cache:
providers:
user_data:
namespace: app_user_data_
Serialization Issues
Serializable or use __sleep()/__wakeup():
class MyObject implements \Serializable
{
public function serialize() { /* ... */ }
public function unserialize($data) { /* ... */ }
}
Cache Misses Enable logging to debug cache behavior:
doctrine_cache:
providers:
my_provider:
logger: true # Logs cache hits/misses
Stale Data Manually clear the cache directory or use:
$this->cacheProvider->deleteAll(); // Clear all cached data
Custom Drivers
Extend Doctrine\Common\Cache\CacheProvider for custom storage backends:
class MyCacheProvider extends CacheProvider
{
public function fetch($id) { /* Custom logic */ }
public function save($id, $data, $lifeTime) { /* Custom logic */ }
}
Tag-Based Invalidation Implement tagging for granular cache invalidation:
$this->cacheProvider->save('tagged_key', $data, 3600, ['users']);
$this->cacheProvider->deleteByTag('users'); // Invalidate all tagged keys
Cache Warmers Preload cache during deployment:
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
class DoctrineCacheWarmer implements CacheWarmerInterface
{
public function warmUp($cacheDir)
{
$this->cacheProvider->save('preloaded_data', $data, 86400);
}
}
How can I help you explore Laravel packages today?