saloonphp/cache-plugin
Laravel-friendly Saloon plugin that adds transparent response caching to your API requests. Supports configurable cache keys, TTL, and cache stores, helping reduce duplicate calls, speed up apps, and respect rate limits with minimal setup.
Installation
composer require saloonphp/cache-plugin
Register the plugin in your Saloon v4 HTTP client:
use Saloon\Plugins\CachePlugin;
$client = new \Saloon\HttpClient([
CachePlugin::class,
]);
First Use Case Cache a simple GET request with a 5-minute TTL (Saloon v4 syntax):
$response = $client->send(new GetUserRequest('123'));
$response->isCached(); // Check if cached (v4 method)
$response->forceFresh(); // Force fresh request (v4 method)
Where to Look First
CachePlugin::class source in src/Plugins/CachePlugin.php (check for v4 compatibility)config/saloon.php (if published; verify Saloon v4 config structure)Basic Caching (Saloon v4)
$response = $client->send(new GetPostsRequest());
// Automatically cached with default TTL (e.g., 1 hour)
Custom TTL per Request (Saloon v4)
$request = new GetUserRequest('123');
$request->withCacheTTL(300); // 5 minutes (unchanged)
$response = $client->send($request);
Conditional Caching (Saloon v4)
$request = new GetUserRequest('123');
$request->shouldCache(function ($request) {
return $request->userId !== 'admin';
});
Cache Invalidation (Saloon v4)
$client->send(new InvalidateUserCacheRequest('123'));
// Or manually (unchanged):
Cache::forget("saloon_{$request->endpoint()}_{$request->userId}");
HttpClient extends \Saloon\HttpClient (not Saloon\Client).config/cache.php uses a supported driver (e.g., file, redis).endpoint() or requestId() (Saloon v4 methods) to avoid collisions.CachePlugin::DEBUG_MODE to log cache hits/misses (unchanged).Cache Key Collisions (Saloon v4)
Default key format: saloon_{endpoint}_{requestId}.
Override with cacheKey() in your request (unchanged):
public function cacheKey(): string {
return "custom_key_{$this->userId}";
}
TTL Misconfiguration (Saloon v4)
Default TTL is null (no caching). Explicitly set TTL (unchanged):
$request->withCacheTTL(60); // 1 minute
Saloon v4 Breaking Changes
cached() → isCached().fresh() → forceFresh().HttpClient extends \Saloon\HttpClient (not legacy Saloon\Client).Stale Data in Distributed Systems (unchanged)
Use Cache::rememberForever() for immutable data or shorter TTLs for volatile data.
CachePlugin::setDebugMode(true);
php artisan cache:clear
Or programmatically:
Cache::flush();
Custom Cache Store (unchanged) Override in plugin config:
'cache' => [
'store' => 'redis', // or 'database', 'memcached'
],
Cache Events (unchanged) Listen for hits/misses:
CachePlugin::onCacheHit(function ($response) {
Log::info("Cache hit for {$response->endpoint()}");
});
Pre-Cache Responses (unchanged) Manually cache responses:
Cache::put($request->cacheKey(), $data, $ttl);
How can I help you explore Laravel packages today?