Web; code

Graphic design; video

Caching HTTP requests with Laravel 5.5 and Guzzle 6

We needed a simple and unobtrusive way to cache our multiple API queries within Laravel. It turns out this is quite easy with Guzzle. Just call the class beneath like so:

$guzzleHandlerStack = \GuzzleHttp\HandlerStack:create();
$cache = \Illuminate\Support\Facades\Cache::store('database');
$guzzleHandlerStack->push(new GuzzleResponseCache($cache), 86400);
$client = new \GuzzleHttp\Client([
    handler => $guzzleHandlerStack

This will cache all requests done with $client for 24 hours in the database—provided you have migrated a cache table with artisan.

News; blog