Weave 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

Resultcachebundle Laravel Package

xlabs/resultcachebundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require xlabs/resultcachebundle
    

    Add to config/bundles.php (Symfony) or config/app.php (Laravel via bridge):

    return [
        // ...
        XLabs\ResultCacheBundle\XLabsResultCacheBundle::class => ['all' => true],
    ];
    
  2. Configuration Edit config/packages/xlabs_resultcache.yaml (Symfony) or config/resultcache.php (Laravel):

    xlabs_resultcache:
        driver: 'doctrine' # or 'apcu', 'memcache', 'redis'
        ttl: 3600          # Default TTL in seconds
        namespace: 'app.'  # Optional namespace prefix
    
  3. First Use Case Enable caching for a repository query:

    // In a Symfony service or Laravel service provider
    $entityManager->getRepository(MyEntity::class)
        ->createQueryBuilder('e')
        ->enableResultCache(); // Enable caching for this query
    

Implementation Patterns

Query Caching Workflows

  1. Repository-Level Caching

    // Symfony
    $qb = $em->getRepository(MyEntity::class)->createQueryBuilder('e');
    $qb->enableResultCache(); // Cache results for this query
    $results = $qb->getQuery()->getResult();
    
    // Laravel (via bridge)
    $results = MyEntity::query()
        ->enableResultCache()
        ->get();
    
  2. Dynamic TTL per Query

    $qb->enableResultCache(600); // Override default TTL (10 mins)
    
  3. Cache Invalidation

    // Invalidate all cached queries for an entity
    $em->getRepository(MyEntity::class)->invalidateResultCache();
    
    // Invalidate specific query (requires unique cache key)
    $qb->invalidateResultCache();
    

Integration Tips

  • Laravel Bridge: Use XLabs\ResultCacheBundle\Laravel\ResultCacheServiceProvider for seamless Laravel integration.
  • Doctrine Events: Listen to onFlush to invalidate caches before writes:
    $em->getEventManager()->addEventListener(
        \Doctrine\ORM\Events::onFlush,
        function () use ($em) {
            $em->getRepository(MyEntity::class)->invalidateResultCache();
        }
    );
    
  • Query Complexity: Cache only SELECT queries with predictable results (avoid ORDER BY with dynamic columns or LIMIT/OFFSET with large datasets).

Gotchas and Tips

Pitfalls

  1. Cache Key Collisions

    • Issue: Identical queries with different parameters (e.g., WHERE id = ?) may generate the same cache key.
    • Fix: Use ->setParameter('id', $id) explicitly and ensure parameters are unique per query.
  2. TTL Misconfiguration

    • Issue: Setting ttl: 0 disables caching entirely. Negative values throw exceptions.
    • Fix: Validate TTL in config or runtime:
      $ttl = max(1, $ttl); // Ensure minimum TTL of 1 second
      
  3. Doctrine Proxy Conflicts

    • Issue: Cached results may include proxy objects that fail to load in subsequent requests.
    • Fix: Use ->getScalarResult() or ->getSingleScalarResult() for simple queries, or hydrate manually:
      $results = $qb->getQuery()->getArrayResult();
      

Debugging

  • Cache Hit/Miss Logging Enable debug mode in config:

    xlabs_resultcache:
        debug: true
    

    Logs appear in Symfony’s profiler or Laravel’s log channel.

  • Cache Key Inspection Dump the generated cache key:

    $qb->enableResultCache();
    $query = $qb->getQuery();
    $cacheKey = $query->getCacheKey(); // Inspect key
    

Extension Points

  1. Custom Cache Drivers Implement XLabs\ResultCacheBundle\Cache\CacheDriverInterface for custom backends (e.g., DynamoDB):

    class DynamoDbDriver implements CacheDriverInterface {
        public function get($key) { /* ... */ }
        public function set($key, $data, $ttl) { /* ... */ }
        public function delete($key) { /* ... */ }
    }
    
  2. Query Filtering Override XLabs\ResultCacheBundle\Cache\QueryCacheKeyGenerator to exclude queries from caching:

    protected function shouldCache(\Doctrine\ORM\QueryBuilder $qb) {
        return !$qb->getQuery()->getHint('cacheable', false);
    }
    
  3. Event Listeners Extend cache invalidation logic:

    $em->getEventManager()->addEventListener(
        \Doctrine\ORM\Events::postUpdate,
        function ($event) {
            $event->getObject()->getClass()->getRepository()
                ->invalidateResultCache();
        }
    );
    
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.
steadrun/laravel-monitor
labyrinthe/connect-laravel
elmogy/layan
drewdan/sentinel-client
dskripchenko/laravel-llm
fatryst/nuonuo
stevenay/myanfont
gmostafa/php-graphql-client
xeviant/paystack
snowsoft/faker
meditationsteps/blogetc
animalyoung/laravel-influxdb
onix-solutions/sms-telera
lonely-lights/prosetta
oxygen/data
oxygen/core
jengo/base
xiloryx/log-collector
a21ns1g4ts/filament-stripe
la-php/laravel-admin