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.
Start by installing the package via Composer: composer require php-http/cache-plugin. You’ll also need a PSR-6 cache implementation (e.g., cache/simple-filesystem-adapter) and a PSR-18 HTTP client (or HTTPlug client). Integrate it by wrapping your HTTP client with the CachePlugin, providing a cache pool and a CacheKeyGenerator. For example:
use Http\Client\HttpClient;
use Http\Plug\CachePlugin;
use Http\Plug\Discovery\ClientDiscovery;
use Http\Plug\Discovery\Exception\NotFoundException;
use Cache\Adapter\PHPArray\ArrayCachePool;
try {
$client = ClientDiscovery::find();
$cachePool = new ArrayCachePool();
$cachePlugin = new CachePlugin($cachePool);
$cachedClient = $cachePlugin->attachTo($client);
} catch (NotFoundException $e) {
// Handle missing client
}
Your first use case is likely caching GET responses for static API data—this plugin automatically honors Cache-Control headers and implements RFC 7234 validation/revalidation.
CachePlugin to any HTTPlug-compatible client (e.g., Guzzle, Symfony HttpClient) without modifying core client logic.CacheKeyGenerator or custom CachePolicy to define which requests to cache (e.g., exclude authenticated endpoints). For granular control, pass RequestOptions with custom metadata to override cache behavior per request.ArrayCachePool with MemcachedCachePool or Redis-backed pools in production, while using MockPlugin + empty cache for tests.CacheKeyGenerator includes all relevant request attributes (method, uri, headers, body for POST/PUT) to avoid unsafe caching of non-idempotent requests.stale-while-revalidate directives—configure TTLs and background revalidation carefully to avoid serving stale data under load.Cache\Logger\CacheLogger). Log cache misses and revalidation attempts.AbstractCachePolicy to implement domain-specific rules (e.g., ignore query params like utm_*, or force-cache for known-unchanging resources).GarbageCollectionPolicy if using filesystem or database-backed pools.How can I help you explore Laravel packages today?