gpt4 book ai didi

php - 缓存 API 数据 Laravel 5.2

转载 作者:行者123 更新时间:2023-12-03 15:46:16 24 4
gpt4 key购买 nike

我正在研究 Halo 5 API。我申请了更高的 API 速率限制,我得到的回应之一是:

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

最佳答案

你可以使用 Laravel 缓存。

试试这个:

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

关于php - 缓存 API 数据 Laravel 5.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37146053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com