In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Know how to implement list in redis? This problem may be often seen in our daily study or work. I hope you can gain a lot from this question. The following is the reference content that the editor brings to you, let's take a look at it!
A Redis list (list) is a simple list of strings sorted in the order in which they are inserted. You can add an element to the head (left) or tail (right) of a list, and a list can contain up to 4294967295 elements (more than 4 billion elements each).
List
Common commands:
Lpush,rpush,lpop,rpop,lrange,BLPOP (blocking version), etc.
Application scenarios:
There are many application scenarios of Redis list, and it is one of the most important data structures of Redis.
We can easily achieve functions such as ranking the latest news.
Another application of Lists is message queue, which can use the PUSH operation of Lists to store the task in Lists, and then the worker thread uses the POP operation to fetch the task for execution.
Implementation method:
The implementation of Redis list is a two-way linked list, that is, it can support reverse search and traversal, which is more convenient to operate, but it brings some additional memory overhead. Many implementations within Redis, including send buffer queues, also use this data structure.
RPOPLPUSH source destination
The command RPOPLPUSH performs the following two actions in one atomic time:
Pops up the last element (the tail element) in the list source and returns it to the client.
Insert the source pop-up element into the list destination as the header element of the destination list.
If source and destination are the same, the footer element in the list is moved to the header and returned, which can be thought of as a rotation operation of the list.
A typical example is the server monitor: they need to check a group of websites in parallel in the shortest possible time to make sure they are accessible.
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
Suppose you now have three lists: job, command, and request, where job does not exist, and command and request both hold non-empty lists. Consider the following command:
BLPOP job command request 30 # blocks for 30 seconds, 0 means indefinitely blocking, the job list is empty, is skipped, and then the first element of the command list is popped up.
1) list to which "command" # pop-up elements belong
2) the value to which the "update system..." # pop-up element belongs
Why block the version of pop, mainly to avoid polling. Let's take a simple example if we use list to implement a work queue. The thread that executes the task can call the blocking version of pop to get the task so that polling can be avoided to check for the existence of the task. When the task comes, the worker thread can return immediately, or it can avoid the delay caused by polling.
Thank you for reading! After reading the above, do you have a general understanding of the implementation of list in redis? I hope the content of the article will be helpful to all of you. If you want to know more about the relevant articles, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
VpSBt5Ub8LbpWX1tnzHFOV6ejwzov/VliqLXau21e6vTPdfl/McOz4ckw62Dr771gAgG+BhGy37x8dfzflXwGf7Vjt7Hcxvw1365
© 2024 shulou.com SLNews company. All rights reserved.