Dav*_*vid 1 php json caching laravel
我正在研究Halo 5 API.我申请获得更高的API速率限制,我得到的答案之一是:
您能否向我们提供有关您对API进行哪些调用以及是否使用任何缓存的更多详细信息?具体来说,我们建议缓存匹配和事件详细信息和元数据,因为这些很少更改.
当他们说"哪个叫你正在制作"时,我得到了这个部分,但是缓存部分,我从来没有使用过.我得到了缓存的基本部分,它加速了你的API,但我不知道如何将它实现到我的API中.
我想知道如何在我的应用程序中缓存一些数据.这是我如何通过API获得玩家奖牌的基本示例.
路线:
Route::group(['middleware' => ['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' => 'player-stats',
'uses' => 'StatsController@index'
]);
});
Run Code Online (Sandbox Code Playgroud)
我的GetDataController调用API Header获取玩家奖牌:
<?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;
}
}
Run Code Online (Sandbox Code Playgroud)
我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是将玩家奖牌显示在视图中的功能:
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)
}
Run Code Online (Sandbox Code Playgroud)
你们知道如何缓存这些数据吗?
(仅供参考,JSON调用大约有189个不同的奖牌,所以它是一个非常大的API调用).我还阅读了有关Laravel缓存的文档,但仍需要澄清.
你可以使用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('medals');
} else {
app('App\Http\Controllers\GetData\GetDataController')->getPlayerArenaStats($gamertag);
}
...
Run Code Online (Sandbox Code Playgroud)
当然,您需要做得比这更好,只存储您需要的信息.请查看以下文档:https://laravel.com/docs/5.1/cache