Laravel: Redis cache in background issue

  caching, cron, laravel, php, redis

I have a schedule task which runs every 5 minutes.

This schedule command puts some data to Redis cache via controller.

Kernel.php

...
protected $commands = [
   CommandsDataCommand::class,
];

protected function schedule(Schedule $schedule)
{
   $schedule->command('data:cache')->everyFiveMinutes()->onOneServer()->runInBackground();
}

DataCommand.php

...
public function handle()
{
   app('AppHttpControllersDataProcessing')->setCache();
}

DataProcessing.php

...
public function setCache()
{
   $data = ['one', 'two', 'three'];
   Cache::store('redis')->put('data', $data, now()->addMinutes(10));
}

This doesn’t work with runInBackground. When I am trying to get data from the cache it returns null anyway.

So, when I remove ->runInBackground() it works perfectly and then I can get data from Redis cache as expected.

The problem that it executes sequentially in foreground but I want to run all schedule tasks simultaneously in background.

Why I can’t access cache when task is running in background and how to overcome this issue?

Source: Laravel

Leave a Reply