friendsofsymfony/http-cache
PHP library to integrate apps with HTTP caching proxies (Varnish, NGINX, Symfony HttpCache, Fastly, Cloudflare). Send efficient cache invalidation/purge and tag requests, abstract proxy features, and test caching/invalidation with PHPUnit tools.
POST/PUT/DELETE operations).AppServiceProvider) to register the HttpCacheInvalidator and related services.HttpCacheMiddleware can be directly integrated into Laravel’s middleware stack (e.g., Kernel.php).symfony/event-dispatcher), but custom event listeners may be needed for Laravel-specific hooks (e.g., Model::saved).| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Symfony Dependency | Medium | Abstract Symfony-specific components (e.g., EventDispatcher) behind interfaces. |
| Middleware Conflicts | Low | Test with Laravel’s middleware priority system (e.g., web vs. api groups). |
| Proxy API Changes | Medium | Use feature flags for proxy-specific logic and monitor for breaking changes. |
| Testing Complexity | High | Leverage the package’s mocking utilities but supplement with Laravel’s HttpTests. |
| Performance Overhead | Low | Benchmark invalidation requests; optimize with async processing (e.g., queues). |
/posts/1) or tag-based (e.g., posts)?HttpCacheInvalidator and proxy adapters.HttpCacheMiddleware before route handling (to validate cache headers).Event facade or bridge Symfony’s EventDispatcher.Purge or Ban endpoints.HttpClient adapters (e.g., Guzzle-based).cache_tags table (if using tag-based invalidation).MockProxyClient in unit tests.ModelObserver for Eloquent).| Component | Compatibility Notes |
|---|---|
| Laravel 10+ | Fully compatible; minor adjustments for Symfony components (e.g., HttpFoundation). |
| PHP 8.1+ | Required for modern Laravel versions; no breaking changes expected. |
| Symfony Components | HttpFoundation, EventDispatcher, and HttpClient are optional dependencies. |
| Varnish/Nginx | Native support; ensure proxy version aligns with package’s tested versions. |
| Cloudflare/Fastly | Requires custom HTTP clients (not bundled). |
composer require friendsofsymfony/http-cache.symfony/http-foundation) via replace in composer.json.AppServiceProvider::boot():
$this->app->singleton(HttpCacheInvalidator::class, function ($app) {
return new HttpCacheInvalidator(
new PurgeClient($app['http.client'], 'http://varnish:6082'),
$app['events']
);
});
app/Http/Kernel.php before route middleware:
protected $middleware = [
\FOS\HttpCache\HttpCacheMiddleware::class,
];
route('cache_purge', ['path' => '/posts/1']).eloquent.saved and call $invalidator->invalidatePath('/posts/1').$mockProxy = new MockProxyClient();
$invalidator = new HttpCacheInvalidator($mockProxy);
HttpFoundation) for breaking changes.composer.json if proxy APIs are unstable.CloudflarePurgeAdapter)./health/cache) to verify proxy connectivity.How can I help you explore Laravel packages today?