2016-05-10 12 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; 
    } 


} 

をとPlayersメダルを表示する機能です:

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

laravelのデフォルトキャッシュシステムやredisのようなものを使用すると、結果を約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