v2board/app/Console/Commands/V2boardCache.php

71 lines
1.6 KiB
PHP
Raw Normal View History

2019-11-30 17:53:54 +08:00
<?php
namespace App\Console\Commands;
use App\Utils\CacheKey;
2019-11-30 17:53:54 +08:00
use Illuminate\Console\Command;
2019-12-17 16:15:36 +08:00
use App\Models\ServerLog;
2020-06-05 01:16:27 +08:00
use App\Models\ServerStat;
2020-01-12 01:27:36 +08:00
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
2019-11-30 17:53:54 +08:00
2019-12-30 21:05:48 +08:00
class V2boardCache extends Command
2019-11-30 17:53:54 +08:00
{
/**
* The name and signature of the console command.
*
* @var string
*/
2019-12-30 21:05:48 +08:00
protected $signature = 'v2board:cache';
2019-11-30 17:53:54 +08:00
/**
* The console command description.
*
* @var string
*/
2019-12-30 21:05:48 +08:00
protected $description = '缓存任务';
2019-11-30 17:53:54 +08:00
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
2020-06-05 01:13:45 +08:00
$this->cacheServerStat();
}
2020-06-05 01:13:45 +08:00
private function cacheServerStat()
{
$serverLogs = ServerLog::select(
'server_id',
DB::raw("sum(u) as u"),
DB::raw("sum(d) as d"),
DB::raw("count(*) as online")
)
->where('updated_at', '>=', time() - 3600)
->groupBy('server_id')
->get();
foreach ($serverLogs as $serverLog) {
$data = [
'server_id' => $serverLog->server_id,
'u' => $serverLog->u,
'd' => $serverLog->d,
'online' => $serverLog->online
];
2020-06-05 01:13:45 +08:00
Cache::put(CacheKey::get('SERVER_STAT', $serverLog->server_id), json_encode($data), 3600);
2020-06-05 01:16:27 +08:00
ServerStat::create($data);
}
2019-11-30 17:53:54 +08:00
}
}