PHP code example of eg-mohamed / laravel-http-client-cache

1. Go to this page and download the library: Download eg-mohamed/laravel-http-client-cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eg-mohamed / laravel-http-client-cache example snippets


use Illuminate\Support\Facades\Http;

$response = Http::cache('products-api', 600)
    ->get('https://api.example.com/products');

$response->fromCache(); // false on the first call, true on subsequent calls

$response = Http::cache('weather-today', 600)
    ->get('https://api.example.com/weather');

$response->fromCache(); // bool
$response->json();       // works exactly like a normal response

$response = Http::cache('currency-rates', [60, 300])
    ->get('https://api.example.com/rates');

$response = Http::cache('erp-report', 600, methods: ['GET', 'POST'])
    ->post('https://legacy-erp.example.com/report', $payload);

$response = Http::cache('products-api', 600)->get($url);

if ($response->fromCache()) {
    // served from cache
}

Http::cache('github-releases', 600)
    ->cacheStore('redis')
    ->get('https://api.github.com/repos/laravel/framework/releases');

Http::cache('github-releases', 600)
    ->cacheStore('redis')
    ->cacheTags(['github', 'releases'])
    ->get($url);

Http::cache('products-api', 600)
    ->cacheWhen(fn ($response) => $response->successful() && $response->json('available'))
    ->get($url);

Http::cache('products-api', 600)
    ->cacheStatuses([200, 201])
    ->get($url);

Http::cache('products-api', 600)
    ->dontCache()
    ->get($url);

Http::cache('products-api', 600, methods: ['GET'])
    ->cacheStore('redis')
    ->cacheTags(['external-api', 'products'])
    ->cacheKeyPrefix('http-client')
    ->cacheWhen(fn ($response) => $response->successful())
    ->cacheStatuses([200])
    ->cacheMethods(['GET', 'POST'])
    ->get($url);

return [
    'enabled' => true,                 // Master switch for the package
    'default_store' => null,           // Default cache store (null = app default)
    'key_prefix' => 'http-client-cache',
    'default_methods' => ['GET'],      // Methods cached by default
    'cache_successful_only' => true,   // Only cache 2xx responses
    'cache_statuses' => null,          // Restrict caching to these statuses
    'cache_failed' => false,           // Allow caching failed responses
    'respect_no_store' => false,       // Skip caching when the response sends Cache-Control: no-store/no-cache
    'default_ttl' => null,             // Fallback TTL when none is provided
    'tags' => [],                      // Default tags
    'serialize_headers' => true,       // Persist response headers
];

Http::fake(['*' => Http::response(['ok' => true], 200)]);

$first = Http::cache('k', 600)->get('https://api.test/a');
$second = Http::cache('k', 600)->get('https://api.test/a');

expect($first->fromCache())->toBeFalse();
expect($second->fromCache())->toBeTrue();

Http::assertSentCount(1);
bash
php artisan vendor:publish --tag="http-client-cache-config"
bash
php artisan ide-helper:generate