blessing-skin-server/app/Listeners/CachePlayerExists.php

42 lines
971 B
PHP
Raw Normal View History

<?php
namespace App\Listeners;
use Cache;
use App\Events;
use App\Models\Player;
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(Events\CheckPlayerExists $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');
2019-04-19 19:36:36 +08:00
return false;
} else {
return true;
}
} else {
return false;
}
}
public function forget(Events\PlayerWasAdded $event)
{
Cache::forget("notfound-{$event->player->name}");
}
}