Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-17T06:17:24.830Z
Answers.LastActivityDate: 2019-04-17T06:17:24.830Z
Answers.UserDisplayName: Robin Schambach
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-16T16:04:00.317Z
Comments.Text: https://docs.craftcms.com/api/v2/craft-cacheservice.html#public-properties
  
  craft()->cache->set('some_key', 'some_value', 3600);
Comments.UserDisplayName: Jim Wyse
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-16T23:08:12.880Z
Comments.Text: @JimWyse this is v2, couldn't see this in v3??
Comments.UserDisplayName: fxfuture
Tags: <plugin-development><plugins><cache><guzzle>
Title: How to cache a Guzzle Http request?
ThreadText: # 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**: @username_1 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
  
  ```php
  // 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
  ]);
  
  ```
  
  
  ---
  
  