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

Cachebundle Laravel Package

bigpaulie/cachebundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require bigpaulie/cachebundle "dev-master" --prefer-dist
    

    Add to AppKernel.php:

    new bigpaulie\CacheBundle\BigpaulieCacheBundle(),
    
  2. Configure In config.yml:

    imports:
        - { resource: "@BigpaulieCacheBundle/Resources/config/services.yml" }
    

    In parameters.yml:

    memcached_servers:
        - { host: 127.0.0.1, port: 11211 }
    
  3. Enable Caching In config_dev.yml (or environment-specific YAML):

    bigpaulie_cache:
        enable: true
    
  4. First Use Case Inject the cache service into a controller or service:

    use Symfony\Component\DependencyInjection\ContainerInterface;
    
    public function __construct(ContainerInterface $container)
    {
        $this->cache = $container->get('bigpaulie.cache');
    }
    

    Cache a query result:

    $key = 'user_list';
    $data = $this->cache->get($key);
    if (!$data) {
        $data = $this->entityManager->getRepository('App:User')->findAll();
        $this->cache->set($key, $data, 3600); // Cache for 1 hour
    }
    

Implementation Patterns

Core Workflows

  1. Query Caching Use the CacheTrait in repositories or services:

    use bigpaulie\CacheBundle\Cache\CacheTrait;
    
    class UserRepository extends ServiceEntityRepository
    {
        use CacheTrait;
    
        public function findAllCached()
        {
            return $this->cacheGet('user_list', function() {
                return $this->findAll();
            }, 3600);
        }
    }
    
  2. Environment-Specific Caching Disable caching in config_dev.yml:

    bigpaulie_cache:
        enable: false
    
  3. Doctrine Integration Configure Memcached for Doctrine caches in config.yml:

    doctrine:
        orm:
            metadata_cache_driver: { type: service, id: doctrine.cache.driver.memcached }
            query_cache_driver: { type: service, id: doctrine.cache.driver.memcached }
            result_cache_driver: { type: service, id: doctrine.cache.driver.memcached }
    
  4. Custom Cache Keys Generate dynamic keys based on query parameters:

    $key = 'user_list_' . $this->getParameter('locale');
    $this->cache->get($key, function() use ($locale) {
        return $this->findBy(['locale' => $locale]);
    });
    

Integration Tips

  • Symfony Cache Service: Leverage the bigpaulie.cache service for manual caching.
  • Doctrine Cache: Use the bundle’s Memcached drivers for metadata, query, and result caching.
  • TTL (Time-To-Live): Always specify a TTL (e.g., 3600 for 1 hour) to avoid stale data.
  • Cache Invalidation: Manually clear keys when data changes:
    $this->cache->delete('user_list_' . $userId);
    

Gotchas and Tips

Pitfalls

  1. Service Not Found If bigpaulie.cache is unavailable, ensure:

    • services.yml is imported.
    • memcached_servers parameter is defined.
    • The bundle is registered in AppKernel.php.
  2. Doctrine Cache Misconfiguration If Doctrine caches fail silently, verify:

    • Memcached is running (127.0.0.1:11211 by default).
    • The doctrine.cache.driver.memcached service is correctly referenced in config.yml.
  3. Environment Overrides Caching may be disabled in config_dev.yml but enabled in config_prod.yml. Test locally with:

    bigpaulie_cache:
        enable: true  # Force-enable for testing
    
  4. Key Collisions Avoid generic keys (e.g., users). Use namespaced keys:

    $key = 'app.users.active_' . $this->getParameter('env');
    

Debugging

  • Check Cache Storage: Use php bin/console debug:cache to inspect cached items.
  • Log Cache Hits/Misses: Extend the CacheTrait to log:
    use Psr\Log\LoggerInterface;
    
    protected $logger;
    
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }
    
    protected function cacheGet($key, $callback, $ttl)
    {
        $data = $this->cache->get($key);
        if ($data) {
            $this->logger->info('Cache HIT', ['key' => $key]);
        } else {
            $this->logger->info('Cache MISS', ['key' => $key]);
            $data = $callback();
            $this->cache->set($key, $data, $ttl);
        }
        return $data;
    }
    

Extension Points

  1. Custom Cache Drivers Override the default Memcached driver by defining a custom service:

    services:
        app.cache.driver:
            class: AppBundle\Cache\CustomDriver
            arguments: ['@memcached']
    

    Then reference it in Doctrine config:

    doctrine:
        orm:
            result_cache_driver: { type: service, id: app.cache.driver }
    
  2. Cache Events Listen for cache events (e.g., cache.clear) to invalidate related data:

    // In a service
    public function onCacheClear(CacheClearEvent $event)
    {
        $this->cache->delete('user_list_*'); // Wildcard delete
    }
    
  3. Tag-Based Invalidation Implement tagging for granular invalidation:

    $this->cache->set('user:123', $user, 3600, ['users', 'admin']);
    $this->cache->invalidateTags(['users']); // Clear all tagged items
    
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