Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Doctrine Cache Bundle Laravel Package

atheon/doctrine-cache-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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],
    ];
    
  2. 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'
    
  3. 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);
        }
    }
    

Implementation Patterns

Common Workflows

  1. 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
    }
    
  2. 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
    
  3. 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);
    }
    

Integration Tips

  • 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()}");
        }
    }
    

Gotchas and Tips

Pitfalls

  1. Deprecation Warning

    • The bundle is abandoned; prefer manual Doctrine Cache configuration or Symfony’s Cache component.
    • Example manual setup (recommended):
      # 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
      
  2. Driver Compatibility

    • Ensure your driver (e.g., file_system, apcu, redis) is installed and configured.
    • Example for Redis:
      doctrine_cache:
          providers:
              redis_provider:
                  namespace: redis_ns
                  driver: predis
                  options:
                      host: localhost
                      port: 6379
      
  3. Namespace Collisions

    • Use unique namespaces for providers to avoid key conflicts:
      doctrine_cache:
          providers:
              user_data:
                  namespace: app_user_data_
      
  4. Serialization Issues

    • Doctrine Cache serializes data; ensure your objects implement Serializable or use __sleep()/__wakeup():
      class MyObject implements \Serializable
      {
          public function serialize() { /* ... */ }
          public function unserialize($data) { /* ... */ }
      }
      

Debugging

  • 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
    

Extension Points

  1. 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 */ }
    }
    
  2. 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
    
  3. 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);
        }
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky