First, the second interview

I was asked in an interview: What command would I use if I wanted to implement queues in Redis? Lpush/LPOP/Rpush/RPOP, and blPOP/BRPOP. L and R stand for left and right. Push stands for add, also called push. Pop stands for pop. Basically meet the first in, first out (FIFO) effect to complete the basic queue, this GitHub experimental code repository.

Simulate the queue by command

RDM Redis Console connection... The connected. localhost:0>lpush mylist a b c d"4"
localhost:0>rpop mylist
"a"
localhost:0>rpop mylist
"b"
localhost:0>rpop mylist
"c"
localhost:0>rpop mylist
"d"
localhost:0>rpop mylist
null
localhost:0>
Copy the code

Java simulation queue implementation of the producer consumer pattern

Redis configures constants

package io.charles;

public class Constant {
    /** * redis link address */
    public static final String host = "127.0.0.1";
    /** * redis startup port */
    public static final int port = 6379;
    /** * Official queue list name */
    public static final String task_queue = "task-queue";
    /** * Temporary queue list name */
    public static final String tmp_queue = "tmp-queue";
}
Copy the code

TaskProducer simulates producers

package io.charles;

/** * to simulate the producer */

import java.util.Random;
import java.util.UUID;

import redis.clients.jedis.Jedis;

public class TaskProducer implements Runnable {
    Jedis jedis = new Jedis(Constant.host, Constant.port);

    public void run(a) {
        Random random = new Random();
        while (true) {
            try {
                Thread.sleep(random.nextInt(600) + 600);
                // The simulation generates a task
                UUID taskid = UUID.randomUUID();
                // Insert tasks into the task queue: task-queue
                jedis.lpush(Constant.task_queue, taskid.toString());
                System.out.println("Inserted a new task:" + taskid);
            } catch(Exception e) { e.printStackTrace(); }}}}Copy the code

TaskConsumer simulates consumers

package io.charles;

import java.util.Random;

import redis.clients.jedis.Jedis;

/** * simulates the consumer */
public class TaskConsumer implements Runnable {
    Jedis jedis = new Jedis(Constant.host, Constant.port);

    public void run(a) {
        Random random = new Random();

        while (true) {

            // Get a task from the task queue" task-queue" and put the task into the temporary queue" tmp-queue"
            String taskid = jedis.rpoplpush(Constant.task_queue, Constant.tmp_queue);

            // Sleep
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // Simulate the contingencies of success and failure
            if (random.nextInt(13) % 7= =0) {// The probability of simulation failure is 2/13
                // Return the task from tmp-queue to task-queue.
                jedis.rpoplpush(Constant.task_queue, Constant.tmp_queue);
                System.out.println(taskid + "Processing failed, bounced back to task queue");

            } else {// Simulate a successful case
                // Clear this task from temporary queue tmp-queue
                jedis.rpop(Constant.tmp_queue);
                System.out.println(taskid + "Processed successfully, cleared"); }}}}Copy the code

TaskShedulerSystem starts producer and consumer threads

package io.charles;

public class TaskShedulerSystem {
    public static void main(String[] args) throws Exception {

        // Start a producer thread to simulate the creation of a task
        new Thread(new TaskProducer()).start();

        Thread.sleep(15000);

        // Start a thread to simulate the processing of the task
        new Thread(newTaskConsumer()).start(); }}Copy the code

Iii. Reference materials

  1. Redis Chinese command manual
  2. GitHub Experimental repository