blessing-skin-server/app/Listeners/CachePlayerJson.php
2019-04-19 19:36:36 +08:00

35 lines
930 B
PHP

<?php
namespace App\Listeners;
use Cache;
use App\Models\Player;
use App\Events\GetPlayerJson;
use App\Events\PlayerProfileUpdated;
use Illuminate\Contracts\Events\Dispatcher;
class CachePlayerJson
{
public function subscribe(Dispatcher $events)
{
$events->listen(GetPlayerJson::class, [$this, 'remember']);
$events->listen(PlayerProfileUpdated::class, [$this, 'forget']);
}
public function remember(GetPlayerJson $event)
{
$key = "json-{$event->player->pid}-{$event->apiType}";
$content = Cache::rememberForever($key, function () use ($event) {
return $event->player->generateJsonProfile($event->apiType);
});
return $content;
}
public function forget(PlayerProfileUpdated $event)
{
Cache::forget("json-{$event->player->pid}-".Player::CSL_API);
Cache::forget("json-{$event->player->pid}-".Player::USM_API);
}
}