-
Notifications
You must be signed in to change notification settings - Fork 15
Caching
It is recommend that you cache the response from the API to use the service responsibly.
If you require a high rate of requests against the API, you should contact Krashnz, ShawnCZek, J-M & Kat_pw with a rationale and contact email for high-rate usage.
If you are using Laravel, you can use the built in facade and helpers for caching the result. Below is an example of how you might cache the result if you were searching for a player.
For more information on caching with Laravel, you can view their documention here https://laravel.com/docs/6.x/cache.
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
use TruckersMP\APIClient\Client;
class SearchController extends Controller
{
/**
* Search for the TruckersMP player.
*
* @param Request $request
*
* @return Response
*
* @throws \Http\Client\Exception
* @throws \TruckersMP\APIClient\Exceptions\PageNotFoundException
* @throws \TruckersMP\APIClient\Exceptions\RequestException
*/
public function __invoke(Request $request)
{
$client = new Client();
$needle = $request->input('needle');
/**
* Cache the `Player` model for the specified player for 10 minutes.
*/
$player = Cache::remember('player.' . (string)$needle, 10, function ($client, $needle) {
return $client->player($needle)->get();
});
return view('search', [
'player' => $player
]);
}
}
If you are not using Laravel you can use PhpFastCache to cache the results from the client. For more information on PhpFastCache, please visit the following link https://phpfastcache.com.
The example below is how you could cache the Player
model for the specified player.
<?php
use Phpfastcache\CacheManager;
use Phpfastcache\Config\ConfigurationOption;
use TruckersMP\APIClient\Client;
use TruckersMP\APIClient\Models\Player;
class Search
{
/**
* @var \TruckersMP\APIClient\Client
*/
protected $client;
/**
* @var \Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface
*/
protected $cache;
/**
* Create a new Search instance.
*
* @throws \Phpfastcache\Exceptions\PhpfastcacheDriverCheckException
* @throws \Phpfastcache\Exceptions\PhpfastcacheDriverException
* @throws \Phpfastcache\Exceptions\PhpfastcacheDriverNotFoundException
* @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException
* @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException
* @throws \ReflectionException
*/
public function __construct()
{
$this->client = new Client();
CacheManager::setDefaultConfig(new ConfigurationOption([
'path' => __DIR__ . '/cache',
]));
$this->cache = CacheManager::getInstance();
}
/**
* Search for the player with the specified ID.
*
* @param int $id
*
* @return \TruckersMP\APIClient\Models\Player
*
* @throws \Http\Client\Exception
* @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException
* @throws \TruckersMP\APIClient\Exceptions\PageNotFoundException
* @throws \TruckersMP\APIClient\Exceptions\RequestException
*/
public function player(int $id): Player
{
$player = $this->cache->getItem('player_' . $id);
if (!$player->isHit()) {
/**
* Cache the `Player` model for the specified player for 10 minutes.
*/
$player->set($this->client->player($id)->get())->expiresAfter(600);
$this->cache->save($player);
}
return $player->get();
}
}
If you have any questions about the library, you can create a topic on our forum.
This package is open-source and is licensed under the MIT license.