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
Http Cache Bundle

Http Cache Bundle Laravel Package

friendsofsymfony/http-cache-bundle

Symfony bundle to enhance HTTP caching: configure cache headers by path/controller, tag responses and invalidate by tags, set up invalidation schemes without code, send purge/ban requests efficiently, vary cache by user type, and plug in custom cache clients.

View on GitHub
Deep Wiki
Context7

Use the FOSHttpCache library in your Symfony projects

Frequently asked questions about Http Cache Bundle
Can I use FOSHttpCacheBundle with Laravel instead of Symfony? If so, how?
FOSHttpCacheBundle is designed for Symfony, but you can adapt it for Laravel by manually integrating its core library (FOSHttpCache) via Composer. Replace Symfony’s dependency injection with Laravel’s service container, rewrite event listeners as Laravel events, and configure cache rules in Laravel’s config files. The bundle’s proxy client logic (e.g., Varnish/Nginx) can still be reused with minimal adjustments.
What Laravel versions does FOSHttpCacheBundle officially support?
FOSHttpCacheBundle is Symfony-native, so it doesn’t natively support Laravel. However, its underlying library (FOSHttpCache) works with PHP 8.1+, which aligns with Laravel 9/10. For Laravel integration, you’d need to bridge Symfony components (like HttpKernel) or use a wrapper like `spatie/laravel-symfony-components`. Always test thoroughly, as Laravel’s routing and middleware differ from Symfony’s.
How do I configure cache rules for specific routes in Laravel using this bundle?
Since the bundle targets Symfony, you’d need to create a Laravel service provider to map Symfony’s `CacheRule` configurations to Laravel routes. For example, define a `fos_http_cache` config array in `config/fos_http_cache.php`, then use a middleware to apply rules like `max_age: 3600` or `tags: ['products']` to matched routes. The bundle’s `Cache` attribute (Symfony) would translate to Laravel’s route middleware or a custom decorator.
Does FOSHttpCacheBundle support tag-based invalidation for Laravel’s Eloquent models?
Indirectly, yes. You’d need to manually trigger invalidation via the bundle’s `Purger` service after model updates/deletes. For example, after `Product::destroy($id)`, call `$purger->invalidateTags(['products'])`. Laravel’s Eloquent events (`saved`, `deleted`) can hook into this logic. The bundle’s tag system works with any HTTP cache proxy (Varnish/Nginx) if properly configured.
What’s the best way to test cache invalidation in a Laravel CI environment?
Mock the bundle’s `Purger` and `CacheClient` interfaces in Laravel’s tests. Use PHPUnit’s `createMock()` to simulate cache hits/misses and invalidation calls. For proxy-specific tests (e.g., Varnish), spin up a Docker container with the proxy and verify HTTP headers/responses. Tools like `symfony/panther` can automate browser-based cache validation if needed.
How does FOSHttpCacheBundle handle cache invalidation for user-specific content in Laravel?
The bundle supports role-based or context-aware caching via Symfony’s `Context` system. In Laravel, you’d extend this by passing user roles (e.g., `auth()->user()->roles`) to the cache key or using middleware to set a `X-User-Role` header. Invalidation would then target tags like `user:admin` or `user:guest`. For dynamic content, combine this with Laravel’s cache tags (e.g., `Cache::tags(['user-' . $userId])->put()`).
Are there Laravel alternatives to FOSHttpCacheBundle with similar features?
Yes. For Laravel, consider `spatie/laravel-cache-control` (simpler header management), `laravel-http-cache` (basic HTTP caching), or `spatie/laravel-activitylog` + custom cache invalidation. For advanced proxy support, `spatie/laravel-varnish` integrates with Varnish specifically. If you need tag-based invalidation, you’ll likely need to build a custom solution or adapt Symfony’s FOSHttpCache library directly.
How do I integrate FOSHttpCacheBundle with Cloudflare in a Laravel app?
First, install the `jean-beru/fos-http-cache-cloudflare` package for Cloudflare support. Configure the bundle’s `proxy_client` in Laravel’s `config/fos_http_cache.php` to use Cloudflare’s API token and zone ID. For invalidation, call `$purger->invalidateTags()` or `$purger->purgeUrl()`—the bundle will forward requests to Cloudflare’s API. Ensure your Laravel app’s cache keys align with Cloudflare’s cache groups for consistency.
What performance impact should I expect from FOSHttpCacheBundle in production?
The bundle adds minimal overhead (~1–5ms per request for header generation) but invalidation requests (e.g., HTTP purges) can introduce latency (~50–200ms RTT). Mitigate this by batching invalidations (e.g., queue them with Laravel Queues) or using async invalidation via Symfony Messenger (if adapted). For high-traffic Laravel apps, test under load to ensure your proxy (Varnish/Nginx) can handle the invalidation volume.
Can I use FOSHttpCacheBundle with Laravel’s built-in cache (file/redis) instead of a proxy?
The bundle is designed for HTTP proxies (Varnish/Nginx/Cloudflare), but you can repurpose its logic for Laravel’s cache stores by creating a custom `CacheClient` implementation. Extend the bundle’s `AbstractCacheClient` and override methods like `purge()` to delegate to Laravel’s `Cache::forget()` or `Cache::tags()`. This won’t support proxy-specific features (e.g., `X-Surrogate-Key`), but it enables tag-based invalidation within Laravel’s cache layer.
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.
escalated-dev/escalated-laravel
escalated-dev/locale
vusys/laravel-runabout
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
directorytree/opensearch-client
directorytree/opensearch-adapter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php