Laravel计划任务仅在登录时运行

时间:2018-05-17 09:03:38

标签: php laravel

我在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'));


    }

0 个答案:

没有答案
相关问题