Php Laravel计划任务仅在登录时运行
我在Php Laravel计划任务仅在登录时运行,php,laravel,Php,Laravel,我在Kernel.php中有一个调度任务,它在userController中调用一个名为cronCache的方法。在Laravel Forge Kernel.php: protected function schedule(Schedule $schedule) { // $schedule->command('inspire') // ->hourly(); //check cache every 1 min
Kernel.php
中有一个调度任务,它在userController
中调用一个名为cronCache
的方法。在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'));
}
这条路线上的中间件是什么?它是否受保护?您是否启动了调度程序
php artisan schedule:run
,您如何知道此cron仅在您登录时运行?@Rooneyl我没有为该路由分配任何中间件。@TheAllen我使用的是DB::getQueryLog();并在视图上打印结果。它显示视图是查询数据库还是使用缓存。当我登录时,它将成功缓存并刷新缓存数小时。当我在缓存过期后注销并重新登录时,我可以看到正在我的日志中查询数据库。然后,它将成功启动缓存并继续按设计刷新缓存。