PHP code example of brightfish / caching-guzzle

1. Go to this page and download the library: Download brightfish/caching-guzzle 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/ */

    

brightfish / caching-guzzle example snippets


/** @var \Psr\SimpleCache\CacheInterface $cache */
$cache = app('cache')->store('database'); // Laravel example, but any PSR-16 cache will do

$middleware = new \Brightfish\CachingGuzzle\Middleware($cache);

$stack = \GuzzleHttp\HandlerStack::create();

$stack->push($middleware);

$client = new \GuzzleHttp\Client([
    'handler' => $stack,
    'base_uri' => 'https://example.org/api/'
]);

$middleware = new \Brightfish\CachingGuzzle\Middleware($store, $ttl = 60, $log = true);

$response_1 = $guzzleClient->get('resource', [
    'cache_ttl' => 90
]);

$response_3 = $guzzleClient->post('resource/84', [
    'cache_anew' => true
]);

$response_2 = $guzzleClient->post('resource/84', [
    'cache' => false
]);

$response_4 = $guzzleClient->post('resource/84', [
    'cache_key' => 'my-key',
    'cache_ttl' => -1
]);

# Get response_1 from cache.
$cached_response_1 = $cache->get('//example.org/api/resource');

# Get response_4 from cache.
$cached_response_4 = $cache->get('my-key');

use Brightfish\CachingGuzzle\Client;

/** @var \Psr\SimpleCache\CacheInterface $store */
$psrCompatibleCache = new Cache();

$client = new Client($psrCompatibleCache, [
    'cache_ttl' => 60,	   // default cache duration
    'cache_log' => false,  // log the cache hits
    // Guzzle options:
    'base_uri' => 'https://example.org/api/'
    // ...
]);