<p>You can either cache it globally or in the session</p>

<pre class="lang-php prettyprint-override"><code>// 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
]);
</code></pre>

CC BY-SA 4.0
2019-04-17T06:17:24.830Z
2019-04-17T06:17:24.830Z
Robin Schambach
<p>In my module I have the following:</p>

<pre><code>$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);
</code></pre>

<p>How can I cache this so it only does the request every 5 minutes?</p>

CC BY-SA 4.0
2019-04-16T16:04:00.317Z
https://docs.craftcms.com/api/v2/craft-cacheservice.html#public-properties

craft()->cache->set('some_key', 'some_value', 3600);
Jim Wyse
CC BY-SA 4.0
2019-04-16T23:08:12.880Z
@JimWyse this is v2, couldn't see this in v3??
fxfuture
<plugin-development><plugins><cache><guzzle>
How to cache a Guzzle Http request?
# How to cache a Guzzle Http request?

(Asked by: username_0 on 2019-04-16)

In my module I have the following:
$client = new Client(['base_uri' => myBaseUri]);

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

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

How can I cache this so it only does the request every 5 minutes?

**Tags:** plugin-development, plugins, cache, guzzle
> **Comments:**
> **username_1**: https://docs.craftcms.com/api/v2/craft-cacheservice.html#public-properties  craft()->cache->set('some_key', 'some_value', 3600);  
> **username_0**: @JimWyse this is v2, couldn't see this in v3??  

---

## 1 answers

### Answer by username_2 on 2019-04-17. Score: 5 | Accepted answer

You can either cache it globally or in the session
// your cache key
$key = 'akey';

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

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

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

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


---

