Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Redis basic tutorial Section 6 List

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

List is an internal bi-directional linked list (double linked list) structure, such as the time complexity of adding elements at both ends of the list is O (1). The main functions are push, pop, getting all values in a range, etc. Key is understood as the name of the linked list in the operation.

The maximum length of the linked list is (2 to the 32). We can add and remove elements from the head or tail of the linked list through the push,pop operation. This allows list to be used as both a stack and a queue.

There are blocking versions of list's pop operations. When we [lr] pop a list object, if the list is empty or does not exist, we will immediately return nil. But the blocking version of b[ LR] pop can block, of course, you can add a timeout, and a nil will be returned after the timeout. 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.

LPUSH Key value left insert

RPUSH Key value right insert

LPop key pops up on the left

RPop key eject on the right

BLPOP,BRPOP blocking left / right eject

Lpush

129.223.248.154lpush members jeff (integer) 1129.223.248.154purl 6379 > lpush members jeff (integer) 2129.223.248.154purl 6379 > lpush members mike jeme (integer) 6

Lpop

129.223.248.154VR 6379 > lpop members "raymond" 129.223.248.154purl 6379 > rpop members "ben"

Llen

129.223.248.154 6379 > llen members (integer) 4

Lrange (lrange firstqueue 0-1 lists all element values in list)

129.223.248.154jemery 6379 > lrange members 021) "richard" 2) "jemery" 3) "mike" 129.223.248.154jemery "6379 > llen members (integer) 4129.223.248.154jemery 6379 > lrange members 031)" richard "2)" jemery "3)" mike "4)" jeff "129.223.248.15441)" richard "2)" jemery "3)" mike "4)" jeff "129.223. 248.154mike 6379 > lrange members 0-11) "richard" 2) "jemery" 3) "mike" 4) "jeff" 5) "derek"

Rpop

129.223.248.154lrange members 6379 > rpop members "derek" 129.223.248.154VR 6379 > lpop members "richard" 129.223.248.154lrange members 0-11) "jemery" 2) "mike" 3) "jeff"

Lindex

129.223.248.154V 6379 > lindex members 1 "mike" 129.223.248.154V 6379 > llen members (integer) 3129.223.248.154V 6379 > rpush firstqueue 3 21 (integer) 3129.223.248.154V 6379 > lrange firstqueue 0-11) "3" 2) "2" 3) "1" 129.223.248.154R 6379 > lpush secqueue 3 2 (integer) 2129.223.248.154rpush firstqueue 6379 > lrange secqueue 0-11) "2" 3) "3"

Rpoplpush removes the element from the tail of the first list and adds it to the header of the second list, and finally returns the value of the removed element. The whole operation is atomic. Return nil if the first list is empty or does not exist

129.223.248.154VR 6379 > rpoplpush firstqueue secqueue "1" 129.223.248.154VR 6379 > lrange firstqueue 0-11) "3" 2) "2" 129.223.248.154RV 6379 > lrange secqueue 0-11) "1" 2) "2" 3) "3" 129.223.248.1546379 >

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report