blessing-skin-server/app/Listeners/CachePlayerExists.php
2019-12-14 11:10:37 +08:00

42 lines
923 B
PHP

<?php
namespace App\Listeners;
use App\Events;
use App\Models\Player;
use Cache;
use Illuminate\Events\Dispatcher;
class CachePlayerExists
{
public function subscribe(Dispatcher $events)
{
$events->listen(Events\CheckPlayerExists::class, [$this, 'remember']);
$events->listen(Events\PlayerWasAdded::class, [$this, 'forget']);
}
public function remember($event)
{
$key = "notfound-{$event->playerName}";
if ($event->playerName && is_null(Cache::get($key))) {
$player = Player::where('name', $event->playerName)->first();
if (!$player) {
Cache::forever($key, '1');
return false;
} else {
return true;
}
} else {
return false;
}
}
public function forget($event)
{
Cache::forget("notfound-{$event->player->name}");
}
}