php-http/cache-plugin
PSR-6 cache plugin for HTTPlug clients. Automatically caches HTTP responses (and can serve stale on error) with configurable cache strategies, TTL, and cache key generation. Drop it into your plugin chain to cut latency and reduce repeated requests.
Architecture fit:
EtagCachePlugin adds complexity for ETag-specific use cases but may not be needed for most Laravel applications.Integration feasibility:
php-http/guzzle7-adapter or Symfony’s HttpClient). Composer integration is straightforward, and Laravel’s service container can manage dependencies (cache pool, client, plugin).Http\Adapter\Guzzle7\Client.Technical risk:
respect_response_cache_directives). Misconfiguration (e.g., caching POST requests) could lead to stale data or race conditions.Stack fit:
HttpClient or HTTPlug adapters like php-http/guzzle7-adapter).// config/app.php
'http_client' => Http\Client\HttpClient::class,
'cache' => CacheManager::class,
Migration path:
symfony/http-client or wrap Guzzle with Http\Adapter\Guzzle7\Client.$client = new Http\Client\HttpClient(
new Http\Adapter\Guzzle7\Client(),
['plugins' => [$cachePlugin]]
);
Cache\Adapter\* (e.g., Cache\Adapter\Redis\RedisCachePool).$cachePool = CacheManager::get('redis')->getPool();
CachePlugin in a Laravel service provider or container binding.$this->app->bind(HttpClient::class, function ($app) {
$client = new Http\Client\HttpClient();
$cachePool = $app['cache']->getPool();
return (new CachePlugin($cachePool))->attachTo($client);
});
Compatibility:
Sequencing:
CachePlugin for non-critical external APIs (e.g., rate-limited services).CacheKeyGenerator or CacheListener for Laravel-specific logic (e.g., cache invalidation on model updates).default_ttl, respect_response_cache_directives).Maintenance:
X-Cache headers (via AddHeaderCacheListener) to track hit/miss ratios.cache:tags).cache:clear or backend-specific tools (e.g., redis-cli FLUSHDB).Support:
$cachePlugin->setCacheListener(new class implements CacheListener {
public function onCacheHit(ResponseInterface $response) {
logger()->debug('Cache hit', ['key' => $response->getHeaderLine('X-Cache-Key')]);
}
});
blacklisted_paths to exclude problematic endpoints (e.g., /api/webhooks).default_ttl => 0.php-http/retry-plugin) for external API failures.Scaling:
default_ttl or use CachePolicy to reduce memory usage for high-volume APIs.Failure modes:
| Scenario | Impact | Mitigation |
|---|---|---|
| Cache backend failure | Increased API latency | Fallback to default_ttl => 0 |
| Stale cached responses | Inconsistent data | Use EtagCachePlugin or short TTLs |
| Key collisions | Overwritten cache entries | Custom CacheKeyGenerator |
| Serialization errors | Failed cache storage | Ensure PSR-7/PSR-17 compliance |
Ramp-up:
CacheKeyGenerator::HEADER_CACHE_KEY for API routes).php artisan http:test --cached --url="https://api.example.com/data"
respect_response_cache_directives for teams managing API integrations.CacheListener for custom logic (e.g., analytics on cache hits).Costs:
cache:stats).How can I help you explore Laravel packages today?