2016-05-10 113 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