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

Adapter Bundle Laravel Package

cache/adapter-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require cache/adapter-bundle
    

    Add to config/bundles.php (Symfony):

    return [
        // ...
        Cache\AdapterBundle\CacheAdapterBundle::class => ['all' => true],
    ];
    
  2. Basic Configuration: Edit config/packages/cache_adapter.yaml (Symfony) or define in config/services.php (Laravel via Symfony integration):

    cache:
        app: '%env(CACHE_DRIVER)%' # e.g., 'redis', 'apcu', 'filesystem'
        default_lifetime: 3600
        pools:
            redis:
                adapter: cache.redis
                provider: 'redis://localhost'
            filesystem:
                adapter: cache.filesystem
                provider: '%kernel.cache_dir%/cache'
    
  3. First Use Case: Inject the cache pool in a service/controller:

    use Psr\Cache\CacheItemPoolInterface;
    
    class MyService
    {
        public function __construct(private CacheItemPoolInterface $cache)
        {
        }
    
        public function getCachedData(string $key): ?string
        {
            return $this->cache->getItem($key)->get();
        }
    }
    

Implementation Patterns

Common Workflows

  1. Tag-Based Invalidation:

    // Set with tags
    $item = $this->cache->getItem('user:123');
    $item->set('John Doe');
    $item->setTags(['users', 'profile']);
    $this->cache->save($item);
    
    // Clear by tag
    $this->cache->deleteItemsMatchingTag('users');
    
  2. Hierarchical Caching: Configure in config/packages/cache_adapter.yaml:

    cache:
        pools:
            hierarchy:
                adapter: cache.hierarchy
                provider:
                    layers:
                        - cache.redis
                        - cache.filesystem
    
  3. Laravel Integration (via Symfony Bridge):

    • Use symfony/cache in Laravel (if installed):
      $cache = app('cache.app'); // Uses Symfony's CacheItemPoolInterface
      
    • Or manually bind:
      $this->app->bind(CacheItemPoolInterface::class, function ($app) {
          return $app->make('cache.app');
      });
      
  4. Dynamic Pool Selection:

    // Resolve a specific pool
    $redisPool = $this->container->get('cache.redis');
    

Best Practices

  • Use getItem() + save() over set()/get() for consistency with PSR-6.
  • Tagging Strategy: Prefix tags (e.g., user:profile) to avoid collisions.
  • Fallback Layers: Leverage hierarchy for local fallback (e.g., Redis → Filesystem).

Gotchas and Tips

Pitfalls

  1. Deprecated Package:

    • Last release in 2019; verify compatibility with modern Symfony/Laravel.
    • Check for forks (e.g., symfony/cache) if active maintenance is needed.
  2. Configuration Overrides:

    • Symfony’s cache_adapter.yaml may conflict with Laravel’s cache.php. Explicitly define pools to avoid ambiguity.
  3. Tagging Limitations:

    • Not all adapters (e.g., APCu) support tags. Use cache.hierarchy with a tag-aware layer (e.g., Redis).
  4. Lifetime Handling:

    • default_lifetime in config is seconds, not minutes. Set 0 for no expiration.

Debugging Tips

  • Check Registered Pools:
    bin/console debug:container | grep cache
    
  • Log Cache Hits/Misses:
    cache:
        pools:
            redis:
                adapter: cache.redis
                provider: 'redis://localhost'
                debug: true # Enable logging
    

Extension Points

  1. Custom Adapters: Extend Cache\AdapterBundle\DependencyInjection\CacheExtension to add new adapters (e.g., Memcached).

  2. Event Listeners: Subscribe to CacheItemPoolInterface events (e.g., CacheItemPool::itemMissed) via Symfony’s event dispatcher.

  3. Laravel-Specific: Override the service provider to integrate with Laravel’s cache manager:

    $this->app->extend('cache', function ($app, $value) {
        return $app->make('cache.app'); // Map Laravel's cache to PSR-6
    });
    

Performance Notes

  • Filesystem Adapter: Use cache.filesystem with serializer: 'jms_serializer' for complex data.
  • Redis Cluster: Configure provider: 'redis://cluster:6379' and ensure the adapter supports it.
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