- How do I integrate FOSHttpCache with Laravel for Varnish/Nginx cache invalidation?
- Start by registering the `HttpCacheInvalidator` in your `AppServiceProvider` using Laravel’s service container. Then, integrate the `HttpCacheMiddleware` in your `Kernel.php` to handle cache headers. For Varnish/Nginx, use the built-in `PurgeClient` or `BanClient` adapters, configuring them with your proxy’s endpoint.
- Does FOSHttpCache support Cloudflare or Fastly CDNs in Laravel?
- Yes, but you’ll need to create custom proxy adapters using HTTP clients like Guzzle. The package provides an abstract `ProxyClientInterface`, so you can extend it to handle Cloudflare’s or Fastly’s API endpoints for purging or invalidation. Check the documentation for adapter examples.
- Can I use tag-based invalidation in Laravel with FOSHttpCache?
- Absolutely. FOSHttpCache abstracts tag-based invalidation, so you can associate tags (e.g., `posts`, `users`) with URLs. In Laravel, trigger invalidations via Eloquent events (e.g., `Model::saved`) or API routes. Store tags in a database table or cache if needed for complex workflows.
- What Laravel versions does FOSHttpCache officially support?
- While FOSHttpCache is Symfony-first, it works with Laravel 8.x, 9.x, and 10.x. Some Symfony-specific components (like `EventDispatcher`) may require minor Laravel adaptations, such as binding services in `AppServiceProvider` or using Laravel’s event system as a bridge.
- How do I test cache invalidation logic in Laravel’s PHPUnit?
- Use FOSHttpCache’s `MockProxyClient` to simulate proxy responses in unit tests. For integration tests, spin up a Dockerized Varnish or Nginx instance and leverage the package’s testing utilities. Laravel’s `HttpTests` can also mock proxy interactions for API endpoints.
- What happens if the HTTP cache proxy (e.g., Varnish) is down during invalidation?
- FOSHttpCache allows you to configure fallback behavior. By default, invalidation requests fail gracefully, but you can extend the `ProxyClient` to log failures, retry with exponential backoff, or degrade to a local cache (e.g., Redis) until the proxy is restored.
- Is FOSHttpCache compatible with Laravel’s queue system for async invalidations?
- Yes, you can dispatch invalidation jobs to Laravel’s queue system to avoid blocking HTTP requests. Create a custom job class that uses the `HttpCacheInvalidator` and dispatch it after model updates or API calls. This is ideal for high-traffic Laravel apps.
- Are there alternatives to FarissofSymfony/http-cache for Laravel HTTP caching?
- For Laravel, alternatives include `spatie/laravel-cache-control` (for cache headers) or `laravel-http-cache` (simpler proxy integration). However, FOSHttpCache stands out for its proxy abstraction, tag support, and testing tools, making it better suited for complex caching setups with Varnish, Nginx, or CDNs.
- How do I monitor cache hit/miss ratios or invalidation success in Laravel?
- Extend FOSHttpCache’s `ProxyClient` to log metrics (e.g., using Laravel’s `Log` facade or a monitoring tool like Datadog). Track invalidation responses and cache headers in middleware or service providers. For dashboards, integrate with Laravel Scout or custom analytics.
- Can FOSHttpCache work alongside Laravel’s built-in cache drivers (Redis, Memcached)?
- Yes, FOSHttpCache handles HTTP-level caching (e.g., Varnish, Cloudflare) while Laravel’s cache drivers manage application-layer caching. Use FOSHttpCache for proxy invalidation and Laravel’s cache for session/storage. For tag synchronization, store tags in a shared cache (e.g., Redis) or database.