2016-05-10 142 views
0

我正在研究Halo 5 API。我申請以獲得更高的速率限制的API,並且我得到了其中一個答覆是這樣的:緩存API數據Laravel 5.2

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.

我得到這個角色時,他們說:「這就要求你正在做」,但緩存部分,我從來沒有用過。我得到了緩存的基本部分,它可以加速你的API,但我不知道如何將它實現到我的API中。

我想知道如何緩存我的應用程序中的一些數據。以下是我如何從API獲得玩家獎牌的基本示例。

路線:

Route::group(['middleware' => ['web']], function() { 

    /** Get the Home Page **/ 
    Route::get('/', '[email protected]'); 


    /** Loads ALL the player stats, (including Medals, for this example) **/ 
    Route::post('/Player/Stats', [ 
     'as' => 'player-stats', 
     'uses' => '[email protected]' 
    ]); 

}); 

我GetDataController調用API頭讓玩家獎牌:

<?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->request('GET', $baseURL, [ 
      'headers' => [ 
       'Ocp-Apim-Subscription-Key' => env('Ocp-Apim-Subscription-Key') 
      ] 
     ]); 

     if ($res->getStatusCode() == 200) { 
      return $result = json_decode($res->getBody()); 
     } elseif ($res->getStatusCode() == 404) { 
      return $result = redirect()->route('/'); 
     } 

     return $res; 
    } 

} 

我MedalController從球員獲得獎牌:

<?php 

namespace App\Http\Controllers; 

use GuzzleHttp; 
use App\Http\Controllers\Controller; 

class MedalController extends Controller { 



    public function getArenaMedals($playerArenaMedalStats) { 

     $results = collect($playerArenaMedalStats->Results[0]->Result->ArenaStats->MedalAwards); 

     $array = $results->sortByDesc('Count')->map(function ($item, $key) { 
      return [ 
       'MedalId' => $item->MedalId, 
       'Count' => $item->Count, 
      ]; 
     }); 

     return $array; 
    } 


} 

而且這是顯示球員獎牌的功能:

public function index(Request $request) { 

     // Validate Gamer-tag 
     $this->validate($request, [ 
      'gamertag' => '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')->getPlayerArenaStats($gamertag); 
     $playerArenaMedalStatsArray = app('App\Http\Controllers\MedalController')->getArenaMedals($playerArenaMedalStats); 
     $arenaMedals = json_decode($playerArenaMedalStatsArray, true); 



     return view('player.stats') 
      ->with('arenaMedals', $arenaMedals) 

} 

你們會知道如何緩存這些數據嗎? (僅供參考,JSON調用約有189種不同的獎牌,所以它是一個相當大的API調用)。我還閱讀了有關Laravel緩存的文檔,但仍需要澄清。

+1

使用像redis甚至默認緩存系統laravel,你應該能夠緩存大約5-10分鐘的結果,並看到顯着減少的API調用。基本上,你使用類似於會話的'Cache'類。創建一個鍵/值對,並在一段時間後過期。 https://laravel.com/docs/5.1/cache – jardis

+0

我已閱讀文檔,但我不明白我會這樣做:**創建一個鍵/值對並讓它在一段時間後過期** – David

回答

4

您可以使用Laravel Cache。

試試這個:

public function getPlayerArenaStats($gamertag) { 
.... some code .... 
    $res = $client->request('GET', $baseURL, [ 
     'headers' => [ 
      'Ocp-Apim-Subscription-Key' => env('Ocp-Apim-Subscription-Key') 
     ] 
    ]); 
    Cache::put('medals', $res, 30); //30 minutes 
    .... more code... 
} 

public function index(Request $request) { 
... 
if (Cache::has('medals')) { 
    $playerArenaMedalStats = Cache::get('medal'); 
} else { 
    app('App\Http\Controllers\GetData\GetDataController')->getPlayerArenaStats($gamertag); 
} 
... 

當然,你需要做的比這更好的,只存儲你所需要的信息。在這裏檢查文檔:https://laravel.com/docs/5.1/cache

+0

我做了你做的,但我得到這個錯誤:**緩存商店[獎牌]沒有定義。** – David

+0

編輯,看看 –

+0

好吧,這有助於我更好地理解這一點,謝謝。 – David