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

Cache Bundle Laravel Package

boson/cache-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the bundle via Composer:

    composer require boson/cache-bundle
    

    Enable the bundle in config/bundles.php:

    Boson\CacheBundle\BosonCacheBundle::class => ['all' => true],
    
  2. Basic Configuration Configure the cache driver in config/packages/boson_cache.yaml:

    boson_cache:
        driver: 'cache.app'  # Default Symfony cache driver
        prefix: 'boson_'     # Optional prefix for keys
    
  3. First Use Case Inject the cache service into a controller or service:

    use Boson\CacheBundle\Service\CacheService;
    
    class MyController extends AbstractController
    {
        public function __construct(private CacheService $cache)
        {
        }
    
        public function index()
        {
            $this->cache->set('key', 'value', 3600); // Cache for 1 hour
            $value = $this->cache->get('key');
            return new Response($value);
        }
    }
    

Implementation Patterns

Common Workflows

  1. Caching API Responses

    $response = $this->cache->remember('api_response', 300, function () {
        return $this->httpClient->request('GET', 'https://api.example.com/data');
    });
    
  2. Tag-Based Invalidation

    # config/packages/boson_cache.yaml
    boson_cache:
        tags: ['users', 'products']
    
    $this->cache->set('user_123', $user, 3600, ['users']);
    $this->cache->clearTags(['users']); // Invalidate all 'users' tagged items
    
  3. Integration with Doctrine

    $this->cache->getDoctrineCache()->get('entity_manager', function () {
        return $this->entityManager;
    });
    

Best Practices

  • Key Naming: Use namespaced keys (e.g., users.{id}) to avoid collisions.
  • TTL Strategy: Default to shorter TTLs (e.g., 300s) for dynamic data.
  • Fallback Logic: Use remember() for expensive operations with fallback logic.

Gotchas and Tips

Pitfalls

  1. Driver Mismatch

    • If cache.app is misconfigured, the bundle silently falls back to array driver.
    • Fix: Verify framework.cache in config/packages/framework.yaml.
  2. Tag Invalidation Race Conditions

    • Tags are cleared asynchronously. Use clearTags() sparingly in critical paths.
    • Workaround: Combine with manual key deletion for urgent invalidations.
  3. Prefix Collisions

    • Default prefix (boson_) may conflict with other bundles.
    • Tip: Override in config:
      boson_cache:
          prefix: 'myapp_boson_'
      

Debugging

  • Enable Cache Debugging

    # config/packages/boson_cache.yaml
    boson_cache:
        debug: true
    

    Logs cache hits/misses to var/log/dev.log.

  • Clear Cache Programmatically

    $this->cache->clear(); // Clears all Boson cache items
    

Extension Points

  1. Custom Cache Item Pool Override the default pool in a service:

    # config/services.yaml
    services:
        Boson\CacheBundle\Service\CacheService:
            arguments:
                $pool: '@my_custom.cache_pool'
    
  2. Event Listeners Subscribe to cache events (e.g., CacheItemPoolClearEvent):

    use Boson\CacheBundle\Event\CacheEvents;
    
    $dispatcher->addListener(CacheEvents::CLEAR_TAG, function ($event) {
        // Custom logic on tag invalidation
    });
    
  3. PSR-6 Compliance The bundle uses Symfony’s CacheItemPoolInterface. Extend it for custom storage backends (e.g., Redis, Memcached).

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.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
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