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

Fragment Cache Bundle Laravel Package

andres-montanez/fragment-cache-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require andres-montanez/fragment-cache-bundle
    

    Enable the bundle in app/AppKernel.php:

    new AndresMontanez\FragmentCacheBundle\AndresMontanezFragmentCacheBundle(),
    
  2. First Use Case: Cache a Twig-embedded sub-request (e.g., a footer or sidebar) by annotating the controller:

    use AndresMontanez\FragmentCacheBundle\Annotation\FragmentCache;
    
    class IndexController extends Controller
    {
        /**
         * @FragmentCache()
         */
        public function footerAction()
        {
            return $this->render('AcmeDemoBundle:Index:footer.html.twig');
        }
    }
    

    In Twig:

    {{ render(controller('AcmeDemoBundle:Index:footer')) }}
    
  3. Configuration: Check app/config/config.yml for default cache settings (e.g., fragment_cache_lifetime). Override as needed:

    fragment_cache:
        lifetime: 3600  # 1 hour in seconds
        namespace: 'fragment_'
    

Implementation Patterns

Workflows

  1. Caching Dynamic Fragments: Use annotations with dynamic keys for personalized fragments:

    /**
     * @FragmentCache(key="user_dashboard_$userId")
     */
    public function userDashboardAction($userId)
    {
        // ...
    }
    
  2. Conditional Caching: Skip caching for non-GET requests or when data changes:

    /**
     * @FragmentCache(skipIf="request.method != 'GET' || $isDataUpdated")
     */
    public function dynamicContentAction()
    {
        // ...
    }
    
  3. Integration with Symfony Cache: Leverage Symfony’s cache system (e.g., cache:pool:clear) to invalidate fragments:

    php bin/console cache:clear fragment_
    

Best Practices

  • Avoid Over-Caching: Use short lifetimes (e.g., 300s) for frequently updated fragments.
  • Tag-Based Invalidation: Extend the bundle to support cache tags (e.g., cache:pool:clear fragment_user_*).
  • Debugging: Enable fragment_cache.debug in config to log cache hits/misses.

Gotchas and Tips

Pitfalls

  1. Deprecated Bundle:

    • Last release in 2014 (Symfony 2.x). Test thoroughly with Symfony 2.3–2.8.
    • No Laravel support; use alternatives like spatie/laravel-fragment-cache for Laravel.
  2. Annotation Limitations:

    • Requires PHP 5.3+ annotations (no PHP 8 attributes).
    • Namespace collisions if fragment_cache.namespace isn’t unique.
  3. Cache Invalidation:

    • Manual invalidation required (no built-in event listeners for cache:clear).

Debugging Tips

  • Check Cache Storage: Inspect the cache directory (e.g., var/cache/dev/) for cached fragments.
    ls var/cache/dev/fragment_*
    
  • Log Cache Events: Enable Symfony’s profiler to monitor cache hits/misses:
    fragment_cache:
        debug: true
    

Extension Points

  1. Custom Cache Drivers: Override the default cache pool in AndresMontanez\FragmentCacheBundle\DependencyInjection\Compiler\CachePass. Example: Use Redis instead of filesystem.

  2. Dynamic Keys: Extend the FragmentCache annotation to support closures for key generation:

    /**
     * @FragmentCache(key="dynamic_key_$(someLogic())")
     */
    
  3. Laravel Alternative: For Laravel, use spatie/laravel-fragment-cache with similar patterns:

    use Spatie\FragmentCache\Facades\Fragment;
    
    Fragment::cache('sidebar', now()->addMinutes(30), function () {
        return view('partials.sidebar');
    });
    
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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui