9951 explained code solutions for 126 technologies


predisHow can I use a Redis job queue in a PHP application?


A Redis job queue can be used in a PHP application to process asynchronous tasks in the background. This can be done by pushing tasks onto a Redis queue, and then having a worker process that pulls tasks off the queue and executes them.

Example code

// Push a job to the queue
$redis->rpush('queue', json_encode(['task' => 'sendEmail', 'params' => ['to' => '[email protected]']]));

// Worker process to pull jobs from the queue
while($job = $redis->lpop('queue')) {
    $job = json_decode($job, true);
    switch ($job['task']) {
        case 'sendEmail':
            sendEmail($job['params']['to']);
            break;
    }
}

function sendEmail($to) {
    // Send an email to $to
    echo "Sending an email to $to\n";
}

Output example

Sending an email to [email protected]

Code explanation

  1. $redis->rpush('queue', json_encode(['task' => 'sendEmail', 'params' => ['to' => '[email protected]']])); - This line pushes a job onto the Redis queue. The job is encoded as JSON and contains two pieces of information - the task to be executed, and the parameters to be passed to the task.

  2. while($job = $redis->lpop('queue')) { - This line starts an infinite loop that pulls jobs off the queue and executes them.

  3. $job = json_decode($job, true); - This line decodes the job from JSON.

  4. switch ($job['task']) { - This line checks the task name in the job and executes the appropriate code based on that.

  5. sendEmail($job['params']['to']); - This line calls the sendEmail function and passes it the to parameter from the job.

  6. function sendEmail($to) { - This is the sendEmail function which is called by the code above. It simply prints a message indicating that an email was sent.

Helpful links

Edit this code on GitHub