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

How to use the list command in Redis

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use the list command in Redis". In daily operation, I believe many people have doubts about how to use the list command in Redis. The editor consulted all kinds of materials and sorted out a simple and easy-to-use operation method. I hope it will be helpful to answer the doubt about "how to use the list command in Redis". Next, please follow the editor to study!

Data structure of list

List type is a double-ended linked list structure, with a capacity of 2 to 32 minus one element, that is, more than 4 billion. Its main functions include push, pop, acquisition elements, etc. It is generally used in stack, queue, message queue and other scenarios.

Redis list command actual combat

[lcompar] push- add elements left / right

Syntax: [lampr] push key value [value...]

Inserts one or more elements in a specified key queue as head or tail inserts

127.0.0.1 lpush pushkey 6379 > lpush pushkey 12 3 (integer) 3127.0.0.1 lpush pushkey 6 379 > rpush pushkey 5 (integer) 5127.0.1 rpush pushkey 6379 > integer 0-11) "4" 2) "3" 3) "2" 4) "1" 5) "5"

Lrange- query scope element

Syntax: lrange key start stop

Gets the elements within the specified range of the list

127.0.0.1 lpush products 6379 > lpush products 123 (integer) 3127.0.0.1 lpush products 656 (integer) 6127.0.0.1 lpush products 0-11) "6" 2) "5" 3) "4" 4) "3" 5) "2" 6) "1"

Linsert- inserts an element before and after an element

Syntax: linsert key BEFORE | AFTER pivot value

Insert an element before or after an element in the list

127.0.0.1 lrange products 0-11) "6" 2) "5" 3) "4" 4) "3" 5) "2" 6) "1" 127.0.0.1 integer > lrange products 0-11) "6" 2) "a" 3) "5" 4) "4" 5) "3" 6) "2" 7) "1" "127.0.1) linsert products after a b (integer) 8127.0.1) lrange products 0-11)" 6 "2)" a "3)" b "4)" 5 "5)" 4 "6)" 3 "7)" 2 "8)" 1 "

Llen- get length

Syntax: llen key

Get the list length

127.0.0.1) lrange products 0-11) "6" 2) "a" 3) "b" 4) "5" 5) "4" 6) "3" 7) "2" 8) "1" 127.0.0.1 integer 6379 > llen products (integer) 8

Lindex- gets the element based on the subscript

Syntax: lindex key index

Get the elements in the list through the index

127.0.0.1) lrange products 0-11) "6" 2) "a" 3) "b" 4) "5" 5) "4" 6) "3" 7) "2" 8) "1" 127.0.0.1) lindex products 2 "b"

Lset- sets values according to subscript

Syntax: lset key index value

Set the value of a list element through an index

127.0.1 lrange products 0-11) "6" 2) "a" 3) "b" 4) "5" 5) "4" 6) "3" 7) "2" 8) "1" 127.0.1 6379 > lset products 2 BOK127.0.0.1:6379 > lrange products 0-11) "6" 2) "a" 3) "B" 4) "5" 5) "4" 6) " 3 "7)" 2 "8)" 1 "

Ltrim- intercepts elements

Syntax: ltrim key start end

Intercepts the elements in the specified range of the queue, and deletes the rest.

127.0.0.1 lrange products 0-11) "6" 2) "a" 3) "B" 4) "5" 5) "4" 6) "3" 7) "2" 8) "1" 127.0.1 6379 > ltrim products 0 3OK127.0.0.1:6379 > lrange products 0-11) "6" 2) "a" 3) "B" 4) "5"

Lrem- removes elements

Syntax: lrem key count value

Remove list element

127.0.0.1 lpush test a 6379 > lrange test 1 a 2 a 3 a 4 56 (integer) 10127.0.1 0.1 lrange test 0-1) 6 "2)" 5 "3)" 4 "4)" a "5)" 3 "6)" a "7)" 2 "8)" a "9)" 1 "10)" a "127.0.1) lrem test 3 a (integer) 4127.0.0. 1RV 6379 > lrange test 0-11) "6" 2) "5" 3) "4" 4) "3" 5) "2" 6) "1" 7) "a"

[lhand r] pop- pops up elements from left / right

Syntax: [lampr] pop key

Pops a node element from the head or tail of the queue (returns the element and removes it from the queue)

127.0.0.1 lrange test 0-11) "6" 2) "5" 3) "4" 4) "3" 5) "2" 6) "1" 127.0.0.1 lrange test 6379 > lpop test "6" 127.0.0.1 lrange test 0-11) "5" 2) "4" 3) "3" 4) "2" 5) "1" 127.0.0.1 rpop test "1" 127.0.1 lrange test 6379 > 0-11) "5" 2) "4" 3) "3" 4) "2"

Rpoplpush- removes the right element and adds it to the left of another list

Syntax: rpoplpush source destination

Remove the last element of the list (right), add that element to another list (left) and return

127.0.0.1 (integer) 3127.0.0.1 (integer) 3127.0.0.1) lrange src 0-11) "3" 2) "2" 3) "1" 127.0.0.1 > lpush dst a b c (integer) 3127.0.1 > lrange dst 0-11) "c" 2) "b" 3) "a" 127.0.1 > rpoplpush src dst "1" 127.0.0.16379 > Lrange src 0-11) "3" 2) "2" 127.0.0.1 6379 > lrange dst 0-11) "1" 2) "c" 3) "b" 4) "a"

B [l / r] pop- blocking pops up a left / right element

Syntax: B [l / r] pop key1 [key2...] Timeout

Move out and get the first or last element of the list, and if there are no elements in the list, it blocks the list until the wait times out or until a popup element is found.

127.0.0.1 lpush list1 6379 > lpush list2 a b (integer) 2127.0.0.1 lrange list1 0-11) "2" 2) 1 "127.0.0.1 6379 > lrange list2 0-11)" b "2)" a "127.0.0.1 6379 > blpop list1 list2 101)" list1 "# list of pop-up elements 2)" 2 " # the value to which the pop-up element belongs is 127.0.0.1 blpop list1 list2 6379 > blpop list1 list2 101) "list1" 2) "1" 127.0.0.1 blpop list1 list2 6379 > list2 "2)" b "127.0.0.1 blpop list1 list2 6379 > blpop list1 list2 101)" list2 "2)" a "127.0.0.1blpop list1 list2 6379 > blpop list1 list2 10 (nil) (10.08s) # when the list is empty Just wait for the timeout of 10 seconds to come here, and the study on "how to use the list command in Redis" is over. I hope it can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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