缓存API数据Laravel 5.2(Caching API Data Laravel 5.2)

编程入门 行业动态 更新时间:2024-10-26 19:29:11
缓存API数据Laravel 5.2(Caching API Data Laravel 5.2)

我正在研究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' ]); });

我的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; } }

我的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缓存的文档,但仍需要澄清。

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:

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.

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.

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.

Route:

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' ]); });

My GetDataController to call the API Header to get Players Medals:

<?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; } }

My MedalController to get the Medals from a Player:

<?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; } }

And this is the function to display the Players medals into view:

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) }

Would you guys know how to cache this data?

(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.

最满意答案

您可以使用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); } ...

当然,您需要做得比这更好,只存储您需要的信息。 请查看以下文档: https : //laravel.com/docs/5.1/cache

You could use Laravel Cache.

Try this:

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); } ...

Of course, you will need to do better than this, to store only the information you need. Check the docs here: https://laravel.com/docs/5.1/cache

更多推荐

本文发布于:2023-08-03 01:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382511.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   数据   API   Laravel   Caching

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!