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 Extra

Cache Extra Laravel Package

twig/cache-extra

Twig extension integrating Symfony Cache to cache template fragments. Adds a single cache tag for easy fragment caching in Twig views, improving performance with configurable cache backends via the Symfony Cache component.

View on GitHub
Deep Wiki
Context7

twig/cache-extra is a Twig extension that integrates the Symfony Cache component to cache rendered template fragments. It adds a single, focused cache tag to improve performance by reusing previously generated markup where appropriate.

Key features:

  • Adds a cache Twig tag for fragment caching in templates
  • Built on the Symfony Cache ecosystem for robust cache backends
  • Helps reduce render time by reusing cached output
  • Ideal for partial views and repeated UI components
  • Simple, minimal API surface for easy adoption
Frequently asked questions about Cache Extra
How do I install twig/cache-extra in a Laravel project using Twig?
Run `composer require twig/cache-extra` and ensure your Laravel project uses Twig via `laravel-twig-bridge`. Add the extension to your Twig environment in `config/twig.php` under the `extensions` array. No additional middleware or service providers are needed.
What cache backends does twig/cache-extra support in Laravel?
It leverages Laravel’s native cache drivers (Redis, Memcached, file, database) via Symfony Cache. For granular invalidation, Redis/Memcached are recommended. Configure your Laravel cache drivers in `.env` (e.g., `CACHE_DRIVER=redis`) and ensure the Symfony Cache component is bootstrapped.
Can I use dynamic keys in the cache tag, like {% cache 'user_' ~ user.id %}?
Yes, the `cache` tag supports dynamic keys. Example: `{% cache 'dashboard_' ~ user.id ~ '_' ~ locale, 300 %}`. This enables personalized, multi-tenant, or localized caching—ideal for SaaS or multilingual Laravel apps using Twig.
How does twig/cache-extra handle cache invalidation in Laravel?
It integrates with Laravel’s cache tags (e.g., `Cache::tags(['products'])->flush()`) and PSR-6 compliance. For Redis/Memcached, use tag-based invalidation. For file/database drivers, manually clear keys via `Cache::forget()` or implement a custom invalidation hook.
Will this work with Laravel 10+ and the latest Twig versions?
Yes, `twig/cache-extra` is compatible with Laravel 10+ and Twig 3.x. It depends on Symfony Cache (v6+), which aligns with Laravel’s modern stack. Check the [GitHub repo](https://github.com/twigphp/cache-extra) for version-specific notes.
How do I disable caching in development or debug mode?
Set `APP_DEBUG=true` in `.env` to bypass caching automatically. Alternatively, conditionally wrap the `cache` tag in Twig: `{% if app.debug %}{% include 'uncached.html.twig' %}{% else %}{% cache ... %}{% include 'cached.html.twig' %}{% endcache %}{% endif %}.
What’s the performance impact of using twig/cache-extra?
It significantly reduces render time for repeated fragments (e.g., product grids, dashboards) by reusing cached markup. Benchmark with `APP_DEBUG=false` and monitor Redis memory usage. Avoid over-caching dynamic content to prevent stale data.
Can I use this with Blade templates instead of Twig?
No, this package is Twig-specific. For Blade, use Laravel’s built-in `@cache` directive or a custom solution like `spatie/laravel-cache`. If migrating to Twig, prioritize performance-critical fragments for caching.
How do I test cache behavior in PHPUnit?
Mock Symfony Cache’s `PoolInterface` in tests to simulate hits/misses. Example: `$cachePool = $this->createMock(PoolInterface::class); $cachePool->method('getItem')->willReturn($item);`. Use Laravel’s `Cache::shouldReceive()` for driver-specific tests.
Are there alternatives to twig/cache-extra for Laravel Twig caching?
For Twig, alternatives are limited—this is the most robust Symfony Cache integration. For Blade, use `@cache` or `spatie/laravel-cache`. If you need advanced features (e.g., edge caching), consider `stash/stash` for file-based caching or Varnish/Nginx for HTTP-level caching.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport