php-http/cache-plugin
PSR-6 cache plugin for HTTPlug that adds transparent HTTP response caching to your client. Plug it into the HTTPlug plugin client to cache and reuse responses, reducing network calls and improving performance.
Architecture fit
php-http/guzzle7-adapter).Illuminate\Cache\CacheManager) via PSR-6 adapters (e.g., predis/predis for Redis, cache/array-adapter for testing).blacklisted_paths) enables fine-grained control (e.g., exclude /auth or /webhooks).AddHeaderCacheListener) add observability without custom middleware.Integration feasibility
php-http/guzzle7-adapter, symfony/http-client).cache/array-adapter for testing).Technical risk
respect_cache_headers → respect_response_cache_directives).CacheKeyGenerator customization).Cache-Control headers; requires manual TTL management for non-HTTP-cached data (e.g., database-backed APIs).Key questions
App\Http\Middleware\HandleCors)?Cache facade (e.g., Cache::remember)?If-None-Match) in Laravel’s request pipeline?X-Cache headers be logged via Laravel’s LogResponse middleware?Cache::remember() or packages like spatie/laravel-cache-control.Stack fit
symfony/http-client (via php-http/symfony-client-adapter).php-http/guzzle7-adapter).Http facade (if wrapped in HTTPlug).predis/predis or phpredis/phpredis).php-memcached/memcached).cache/file-adapter).cache/db-adapter).Migration path
composer require php-http/guzzle7-adapter symfony/http-client
Wrap Laravel’s Http client:
use Http\Client\Common\Plugin\PluginStack;
use Http\Client\Common\Plugin\PluginClient;
use Http\Client\Common\Plugin\CachePlugin;
use Http\Client\Common\Plugin\Cache\CachePool;
$client = new PluginClient(
new PluginStack(ClientDiscovery::find()),
[
new CachePlugin(new CachePool(new PredisAdapter()))
]
);
use Cache\Adapter\Redis\RedisCachePool;
use Illuminate\Support\Facades\Cache;
$redis = Cache::store('redis')->getConnection();
$cachePool = new RedisCachePool($redis);
Http::get() with the cached client:
$response = $cachedClient->sendRequest(new Request('GET', 'https://api.example.com/data'));
Compatibility
php-http/guzzle6-adapter (deprecated in favor of v7).Cache-Control headers.Sequencing
X-Cache headers).Maintenance
Support
cache_listeners to log hits/misses:
$plugin = new CachePlugin($cachePool, [
'cache_listeners' => [new AddHeaderCacheListener()]
]);
X-Cache headers in responses (e.g., X-Cache: HIT).blacklisted_paths.default_ttl => 0 to disable caching entirely.Scaling
ab or Laravel Forge).Failure modes
| Scenario | Impact | Mitigation |
|---|---|---|
| Cache backend down | Fallback to live requests | Set default_ttl => 0 as fallback. |
| Corrupted cache entry | null entries ignored (v1.7.3+) |
Use CacheKeyGenerator validation. |
| Stream detachment fails | Response body lost | Test with large payloads (>10MB). |
| TTL misconfiguration | Stale data served | Monitor X-Cache headers. |
Ramp-up
How can I help you explore Laravel packages today?