Queue: Redis
Redis is an open-source, in-memory key-value store used for fast caching, databases, and message brokering, supporting various data structures and offering ultra-low latency.
You can use Redis as a queue driver in Laravel, which is particularly useful for handling asynchronous tasks and more efficient job processing.
Setup Redis
Choose Redis client
Laravel supports two Redis clients: phpredis and predis. The phpredis extension is a native PHP extension, while predis is a pure PHP library (which can be slower).
predis/predis (slower)
To install predis/predis, you can use composer:
composer require predis/predisphpredis (faster)
But you should prefer to use the phpredis extension for better performance. To install it, you can use the following command:
git clone https://github.com/phpredis/phpredis.git
cd phpredis
phpize
./configure
make
sudo make installThen, add the extension to your php.ini file:
extension=redis.soCheck if the extension is loaded:
php -m | grep redisEnable new queue driver
To use Redis as your queue driver, you need to update your .env file:
REDIS_CLIENT=phpredis # `predis` or `phpredis`
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379And set the queue connection to Redis:
QUEUE_CONNECTION=redisCreate a job
You can create a new job using the Artisan command:
php artisan make:job TestJobThis will create a new job class in the app/Jobs directory. You can then implement the handle method to define the job's logic.
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class TestJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $message;
/**
* Create a new job instance.
*/
public function __construct(string $message)
{
$this->message = $message;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// Log dans laravel.log
Log::info('Queue works! Message: '.$this->message);
// Affiche dans le terminal si tu run php artisan queue:work
echo 'Queue works! Message: '.$this->message.PHP_EOL;
}
}Dispatch the job
You can dispatch the job using the dispatch method:
php artisan tinkerThen in the Tinker shell:
App\Jobs\TestJob::dispatch('Hello, Redis!');Process the queue
To process the queue, you can run the following command:
php artisan queue:workLaravel Horizon
Laravel Horizon is a dashboard and configuration tool for managing Redis queues in Laravel applications. It provides a beautiful interface to monitor your queues, jobs, and more.
- Official documentation: Laravel Horizon
- Dashboard URL:
/horizon
composer require laravel/horizonThen publish the Horizon assets:
php artisan horizon:installValet
If you are using Laravel Valet, check your Valet configuration use same PHP version with Redis extension installed.
If you just installed the phpredis extension, you may need to restart Valet:
valet restartAnd check Valet links
valet linksError: Class "Redis" not found
If you encounter the error Class "Redis" not found, it means that the phpredis extension is not installed or not enabled in your PHP configuration.
To resolve it temporarily, you can run the following command to install the predis/predis:
composer require predis/predisAnd enable it in your .env file:
REDIS_CLIENT=predisIf Laravel Horizon works with predis/predis, you can keep using it, but for better performance, it's recommended to install the phpredis extension.
Check now if the phpredis extension is installed:
php -m | grep redisIf it works, Laravel Horizon should be able to connect to Redis without any issues, but if you still encounter the error, you may need to restart your web server or PHP-FPM service (or check if service like Valet is using the correct PHP version with the phpredis extension installed).
Handle jobs with redis-cli
You can also handle jobs directly using the redis-cli command-line tool. This is useful for debugging or manual job processing.
Connect to Redis
To connect to your Redis server, you can use the redis-cli command:
redis-cli -h 127.0.0.1 -p 6379 -n 0View the queue
To view the jobs in the queue, you can use the following command:
redis-cli LRANGE queues:default 0 -1Delete a job
To delete a specific job from the queue, you can use the LREM command:
redis-cli LREM queues:default 0 "job_id"Delete all jobs of a queue
To delete all jobs from the default queue, you can use the DEL command:
redis-cli DEL queues:defaultClear all queues
To clear all queues, you can use the FLUSHDB command, but be cautious as this will remove all data from the current Redis database:
redis-cli FLUSHALL