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")
##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"
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!