- 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.