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.
Pros:
Cons:
fos_http_cache config in config/packages/). Attributes-based configuration (Symfony 5+) reduces XML/YAML verbosity.
composer require friendsofsymfony/http-cache-bundle.fos_http_cache.yaml for rules (e.g., CacheRule for /api/* with max_age: 3600).[Cache(ttl: 3600)] attributes to controllers/actions.proxy_client config.generate_url_type flexibility).@Cache → [Cache]).PurgeTagsListener configuration.jean-beru/fos-http-cache-cloudfront) and runtime secrets (e.g., API tokens).fos_http_cache may lose flash messages if not configured (fixed in 2.10.2+).ContextInvalidationSessionLogoutHandler.request_factory/stream_factory needed for non-standard HTTP clients.Surrogate-Key) or proxy-specific extensions?fos:httpcache:clear command).postPersist).@ApiResource caching strategies.| Current State | Migration Steps | Risks |
|---|---|---|
| No HTTP Caching | 1. Install bundle. 2. Configure fos_http_cache.yaml with basic CacheRules. 3. Add [Cache] attributes to controllers. 4. Deploy proxy (e.g., Varnish). |
Minimal; focus on proxy setup and header validation. |
| Symfony HttpCache | 1. Replace HttpCache with FOSHttpCacheBundle for tag support. 2. Migrate from HttpCacheStore to fos_http_cache config. 3. Update invalidation logic to use PurgeTagsListener. |
Tag-based invalidation may require proxy-specific tweaks. |
| Varnish/Nginx Cache | 1. Update VCL/Nginx config to honor Surrogate-Control headers. 2. Configure proxy_client in bundle. 3. Test tag invalidation (e.g., curl -X PURGE http://proxy/tags:user_123). |
Nginx may need custom PurgeTagsListener for tag support. |
| Custom Cache Headers | 1. Extend FOSHttpCacheBundle via custom CacheRule or ResponseMatcher. 2. Use match_response config for complex logic. 3. Validate headers with tools like curl -I. |
Complex logic may require custom event listeners. |
| Legacy Annotations (Symfony <5) | 1. Replace @Cache annotations with [Cache] attributes. 2. Update fos_http_cache.yaml to remove annotations section. 3. Test all cached routes. |
Attribute migration may miss some routes if not thoroughly tested. |
How can I help you explore Laravel packages today?