saloonphp/cache-plugin
Laravel/PHP caching plugin for Saloon HTTP client. Cache API responses with configurable drivers, TTLs, and cache keys to reduce requests and speed up integrations. Supports per-request caching controls and easy setup for existing Saloon connectors.
Installation
composer require saloonphp/cache-plugin
Register the plugin in your Saloon HTTP client:
use Saloon\Plugins\CachePlugin;
$client = new YourHttpClient([
CachePlugin::class,
]);
First Use Case Cache a simple GET request with a 5-minute TTL:
$response = $client->send(new GetUserRequest('123'));
$response->cached(); // Check if cached
$response->fresh(); // Force fresh request
Where to Look First
CachePlugin::class source code in src/Plugins/CachePlugin.phpconfig/saloon.php (if published)Basic Caching
$response = $client->send(new GetPostsRequest());
// Automatically cached with default TTL (e.g., 1 hour)
Custom TTL per Request
$request = new GetUserRequest('123');
$request->withCacheTTL(300); // 5 minutes
$response = $client->send($request);
Conditional Caching
$request = new GetUserRequest('123');
$request->shouldCache(function ($request) {
return $request->userId !== 'admin';
});
Cache Invalidation
$client->send(new InvalidateUserCacheRequest('123'));
// Or manually:
Cache::forget("saloon_{$request->endpoint()}_{$request->userId}");
config/cache.php is properly configured (e.g., file, redis, or database driver).requestId() or endpoint() to avoid duplicate cached entries for similar requests.Saloon\Plugins\CachePlugin::DEBUG_MODE to log cache hits/misses.Cache Key Collisions
saloon_{endpoint}_{requestId}.cacheKey() in your request:
public function cacheKey(): string {
return "custom_key_{$this->userId}";
}
TTL Misconfiguration
null (no caching). Always explicitly set TTL:
$request->withCacheTTL(60); // 1 minute
Stale Data in Distributed Systems
Cache::rememberForever() for immutable data (e.g., static configs).Race Conditions
Cache::get()/Cache::put(), which are atomic in Laravel.Cache::lock() if needed.CachePlugin::setDebugMode(true);
// Check logs for `CachePlugin` entries.
php artisan cache:clear
Or programmatically:
Cache::flush();
Custom Cache Store Override the default store in the plugin config:
'cache' => [
'store' => 'redis', // or 'database', 'memcached'
],
Cache Events Listen for cache hits/misses:
CachePlugin::onCacheHit(function ($response) {
Log::info("Cache hit for {$response->request()->endpoint()}");
});
Pre-Cache Responses
Use Cache::put() manually before sending requests:
Cache::put($request->cacheKey(), $data, $ttl);
How can I help you explore Laravel packages today?