我在Kernel.php
中有一个计划任务,它调用cronCache
中名为userController
的方法。计划任务每分钟在Laravel Forge
Kernel.php
:
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
//check cache every 1 min
$schedule->call('App\Http\Controllers\userController@cronCache')
->everyMinute();
}
userController@cronCache
:
public function cronCache() {
$vw = Cache::remember('userCountCache',10,function(){
return $query = DB::table('vwUserCount')->first();
});
}
此方法缓存mySQL查询并成功运行,但仅在我登录到我的应用程序时。一旦我注销,任务就会停止运行。当我再次登录时,任务继续再次运行。
我在userController
中有另一个方法,如果加载我的视图时,如果不存在,则会生成相同查询的缓存。我不知道这是否相关,但我在这里包括它:
public function showUserCache() {
$vw = Cache::remember('userCountCache',3,function(){
return $query = DB::table('vwUserCount')->first();
});
$userCount = $vw->userCount;
return view ('backend.user_summary', compact('userCount'));
}