How to cache a Guzzle Http request? In my module I have the following:

$client = new Client(['base_uri' =&gt; myBaseUri]);

$response = $client-&gt;request('GET', myQueryString, [
    'headers' =&gt; [
        'Accept'            =&gt; 'application/json',
        'Accept-Encoding'   =&gt; 'deflate,gzip'
    ],
    'connect_timeout' =&gt; 3,
    'verify' =&gt; false,
    'timeout' =&gt; 5,
    'http_errors' =&gt; false
]);

$json = $response-&gt;getBody();
$data = json_decode($json, true);


How can I cache this so it only does the request every 5 minutes?
You can either cache it globally or in the session

// your cache key
$key = 'akey';

$cache = Craft::$app-&gt;getCache();
$value = $cache-&gt;get($key);
// check if there is a valid value that is not older than 5 minutes
if ($value !== null &amp;&amp; \is_array($value) &amp;&amp; isset($value['time']) &amp;&amp;
    $value['time'] &gt;= time() - 3600) {
    return $value['value'];
}

// make your request
$response = Craft::createGuzzleClient(['base_uri' =&gt; $myBaseUri])
    -&gt;get('GET', myQueryString, [
    'headers' =&gt; [
        'Accept'            =&gt; 'application/json',
        'Accept-Encoding'   =&gt; 'deflate,gzip'
    ],
    'connect_timeout' =&gt; 3,
    'verify' =&gt; false,
    'timeout' =&gt; 5,
    'http_errors' =&gt; false
]);

$json = $response-&gt;getBody();
$data = Json::decode($json, true);

// cache it
$cache-&gt;set($key,  
    'time'  =&gt; time(),
    'value' =&gt; $data
]);