12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Log;
- use Laravel\Sanctum\PersonalAccessToken;
- class CleanExpiredTokens extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'sanctum:clean-expired-tokens';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '定时清理过期的token';
- /**
- * Execute the console command.
- *
- * @return int
- */
- public function handle()
- {
- try {
- while (true) {
- // 过期时间
- $expirationTime = now()->subMinutes(config('sanctum.expiration'));
- // 删除所有过期的 token
- PersonalAccessToken::where('created_at', '<', $expirationTime)->delete();
- // 等待24小时
- sleep(86400);
- }
- } catch (\Exception $e) {
- Log::error($this->description . ' 程序异常: ' . $e->getMessage());
- throw $e;
- }
- }
- }
|