title: Caching API Data Laravel 5.2
body: <p>I'am working on the Halo 5 API. I applied to get a higher rate limit for the API, and one of the responses I got was this:</p>
  
  <blockquote>
    <p>Can you please provide us more details about which calls you’re making to the APIs and if you’re using any caching? Specifically we would recommend caching match and event details and metadata as those rarely change.</p>
  </blockquote>
  
  <p>I get the part when they say "which calls you're making", but the caching part, I have never worked with that. I get the basic parts of caching, that it speeds up your API, but I just wouldn't know how to implement it into my API.</p>
  
  <p>I would want to know how to cache some data in my app. Here is a basic example of how I would get players medals from the API.</p>
  
  <p>Route:</p>
  
  <pre><code>Route::group(['middleware' =&gt; ['web']], function () {
  
      /** Get the Home Page **/
      Route::get('/', 'HomeController@index');
  
  
      /** Loads ALL the player stats, (including Medals, for this example) **/
      Route::post('/Player/Stats', [
          'as' =&gt; 'player-stats',
          'uses' =&gt; 'StatsController@index'
      ]);
  
  });
  </code></pre>
  
  <p>My GetDataController to call the API Header to get Players Medals:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers\GetData;
  
  use Psr\Http\Message\RequestInterface;
  use Psr\Http\Message\ResponseInterface;
  use Psr\Http\Message\UriInterface;
  
  use GuzzleHttp;
  use App\Http\Controllers\Controller;
  
  class GetDataController extends Controller {
  
  
      /**
       * Fetch a Players Arena Stats
       *
       * @param $gamertag
       * @return mixed
       */
      public function getPlayerArenaStats($gamertag) {
  
          $client = new GuzzleHttp\Client();
  
          $baseURL = 'https://www.haloapi.com/stats/h5/servicerecords/arena?players=' . $gamertag;
  
          $res = $client-&gt;request('GET', $baseURL, [
              'headers' =&gt; [
                  'Ocp-Apim-Subscription-Key' =&gt; env('Ocp-Apim-Subscription-Key')
              ]
          ]);
  
          if ($res-&gt;getStatusCode() == 200) {
              return $result = json_decode($res-&gt;getBody());
          } elseif ($res-&gt;getStatusCode() == 404) {
              return $result = redirect()-&gt;route('/');
          }
  
          return $res;
      }
  
  }
  </code></pre>
  
  <p>My MedalController to get the Medals from a Player:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use GuzzleHttp;
  use App\Http\Controllers\Controller;
  
  class MedalController extends Controller {
  
  
  
      public function getArenaMedals($playerArenaMedalStats) {
  
          $results = collect($playerArenaMedalStats-&gt;Results[0]-&gt;Result-&gt;ArenaStats-&gt;MedalAwards);
  
          $array = $results-&gt;sortByDesc('Count')-&gt;map(function ($item, $key) {
              return [
                  'MedalId' =&gt; $item-&gt;MedalId,
                  'Count' =&gt; $item-&gt;Count,
              ];
          });
  
          return $array;
      }
  
  
  }
  </code></pre>
  
  <p>And this is the function to display the Players medals into view:</p>
  
  <pre><code> public function index(Request $request) {
  
          // Validate Gamer-tag
          $this-&gt;validate($request, [
              'gamertag' =&gt; 'required|max:16|min:1',
          ]);
  
          // Get the Gamer-tag inserted into search bar
          $gamertag = Input::get('gamertag');
  
  
  
  
          // Get Players Medal Stats for Arena
          $playerArenaMedalStats = app('App\Http\Controllers\GetData\GetDataController')-&gt;getPlayerArenaStats($gamertag);
          $playerArenaMedalStatsArray = app('App\Http\Controllers\MedalController')-&gt;getArenaMedals($playerArenaMedalStats);
          $arenaMedals = json_decode($playerArenaMedalStatsArray, true);
  
  
  
           return view('player.stats')
              -&gt;with('arenaMedals', $arenaMedals)
  
  }
  </code></pre>
  
  <p>Would you guys know how to cache this data?</p>
  
  <p>(FYI, there are about 189 different medals from the JSON call, so its a pretty big API call). I also read the documentation about caching for Laravel, but still need clarification.</p>