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

doctrine/doctrine-cache-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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.

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

Implementation Patterns

Common Workflows

  1. 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();
    
  2. Metadata Cache: Configure metadata_cache in config/packages/doctrine.yaml:

    doctrine:
        orm:
            metadata_cache_driver: doctrine_cache
    
  3. Query Cache: Enable query caching in config/packages/doctrine.yaml:

    doctrine:
        orm:
            query_cache_driver: doctrine_cache
    

Integration Tips

  • 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'
    

Gotchas and Tips

Pitfalls

  1. Deprecation Warning:

    • The bundle is abandoned; prefer Symfony’s symfony/cache or manual Doctrine Cache setup.
    • Example migration:
      # 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'
      
  2. Namespace Collisions:

    • Ensure namespace in doctrine_cache.yaml is unique (e.g., app.cache).
    • Avoid conflicts with other bundles using Doctrine Cache.
  3. File Permissions:

    • If using file_system provider, ensure %kernel.cache_dir% is writable:
      chmod -R 775 %kernel.cache_dir%
      
  4. Serialization Issues:

    • Doctrine Cache requires objects to be serializable. Use __serializeMagicMethods() or implement Serializable.

Debugging

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

Extension Points

  1. 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);
        }
    }
    
  2. Event Listeners: Listen to cache.clear events (Symfony 4+):

    use Symfony\Component\Cache\CacheEvents;
    
    $cache->on(CacheEvents::CLEAR, function () {
        // Custom logic on cache clear
    });
    
  3. Tag-Based Invalidation: Use Doctrine\Common\Cache\CacheProvider::delete() with tags (if supported by the provider):

    $this->cacheProvider->delete('tag:user_123');
    
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.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware