Home  >  Article  >  Database  >  When to use list in redis

When to use list in redis

藏色散人
藏色散人Original
2019-06-11 09:50:014209browse

Redis list (list) is a simple list of strings, sorted in insertion order. You can add an element to the head (left) or tail (right) of a list, and a list can contain up to 232 - 1 elements (4294967295, more than 4 billion elements per list). (Recommended: "redis video tutorial")

When to use list in redis

##list

Common commands:

lpush, rpush, lpop, rpop, lrange, BLPOP (blocked version), etc.

Application scenarios:

There are many application scenarios for Redis list, and it is also one of the most important data structures of Redis.

We can easily implement functions such as ranking of the latest news.

Another application of Lists is the message queue. You can use the PUSH operation of Lists to store tasks in Lists, and then the worker thread uses the POP operation to take out the tasks for execution.

Implementation method:

The implementation of Redis list is a doubly linked list, which can support reverse search and traversal, and is more convenient to operate. However, It brings some additional memory overhead. Many implementations within Redis, including send buffer queues, etc. also use this data structure.

RPOPLPUSH source destination

The command RPOPLPUSH performs the following two actions within an atomic time:

List source The last element (the tail element) in the element is popped out and returned to the client.

Insert the element popped by source into the list destination as the head element of the destination list.

If source and destination are the same, the tail element in the list is moved to the head and the element is returned. This special case can be regarded as a rotation operation of the list.

A typical example is server monitoring programs: they need to check a group of websites in parallel in the shortest possible time to ensure their accessibility.

    redis.lpush "downstream_ips", "192.168.0.10"  
    redis.lpush "downstream_ips", "192.168.0.11"  
    redis.lpush "downstream_ips", "192.168.0.12"  
    redis.lpush "downstream_ips", "192.168.0.13"  
    Then:  
    next_ip = redis.rpoplpush "downstream_ips", "downstream_ips"

BLPOP

Assume that there are three lists of job, command and request. Job does not exist, and command and request both hold non- Empty list. Consider the following command:

BLPOP job command request 30 #Block for 30 seconds. If it is 0, it will block indefinitely. The job list is empty and is skipped. Then the first element of the command list is popped out.

1) "command"                                                                                                                                                                                                                                         Well, mainly to avoid polling. As a simple example, if we use list to implement a work queue. The thread executing the task can call the blocking version of pop to obtain the task, thus avoiding polling to check whether a task exists. The worker thread can return immediately when the task comes, and can also avoid the delay caused by polling.

The above is the detailed content of When to use list in redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn