当前位置:网站首页>Redis basic data type (list)

Redis basic data type (list)

2022-06-11 21:26:00 Don't like learning since childhood~

Redis Basic data type (List)

stay Redis in , We can list Play into Stack 、 queue 、 Blocking queues !

be-all list Commands are L At the beginning ,Redis Case insensitive commands

127.0.0.1:6379> flushall
OK
127.0.0.1:6379> keys *
(empty array) 
127.0.0.1:6379> LPUSH list one   #  Put one or more values , Insert to the head of the list ( Left )
(integer) 1
127.0.0.1:6379> LPUSH list two
(integer) 2
127.0.0.1:6379> LPUSH list three
(integer) 3
127.0.0.1:6379> LRANGE list 0 -1   #  obtain list The value in 
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> LRANGE list 0 1  #  Get specific values through intervals 
1) "three"
2) "two"
127.0.0.1:6379> RPUSH list righr  # #  Put one or more values , Insert to the head of the list ( Right )
(integer) 4
127.0.0.1:6379> LRANGE list 0 1
1) "three"
2) "two"
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "righr"

########################################################

LPOP
RPOP

127.0.0.1:6379> LPOP list   #  Remove the first element of the list 
"three"
127.0.0.1:6379> RPOP list   #  Remove the last element of the list 
"righr"
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "one"

########################################################

LINDEX

127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "one"
127.0.0.1:6379> LINDEX list 1  #  Get... By subscript list One of the values 
"one"
127.0.0.1:6379> LINDEX list 0
"two"

########################################################
LLEN

127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> LPUSH list one
(integer) 1
127.0.0.1:6379> LPUSH list two
(integer) 2
127.0.0.1:6379> LPUSH list three
(integer) 3
127.0.0.1:6379> LLEN list  #  Returns the length of the list 
(integer) 3

########################################################
 Remove the specified value !
 Take off  uid

127.0.0.1:6379> LPUSH list three
(integer) 4
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "three"
3) "two"
4) "one"
127.0.0.1:6379> LREM list 1 one  #  remove list Of a specified number in a set value, Exactly match 
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "three"
3) "two"
127.0.0.1:6379> LREM list 1 three
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
127.0.0.1:6379> LPUSH list three
(integer) 3
127.0.0.1:6379> LREM list 2 three
(integer) 2
127.0.0.1:6379> LRANGE list 0 -1
1) "two"

########################################################
trim  trim :list  truncation !

127.0.0.1:6379> RPUSH mylist "hello"
(integer) 1
127.0.0.1:6379> RPUSH mylist "hello1"
(integer) 2
127.0.0.1:6379> RPUSH mylist "hello2"
(integer) 3
127.0.0.1:6379> RPUSH mylist "hello3"
(integer) 4
127.0.0.1:6379> LTRIM mylist 1 2  #  Intercept the specified length by subscript , This list Has been changed , Only the intercepted elements are left 
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello1"
2) "hello2"

########################################################
rpoplpush #  Remove the last element of the list , Move it to a new list 

127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> RPUSH mylist "hello"
(integer) 1
127.0.0.1:6379> RPUSH mylist "hello1"
(integer) 2
127.0.0.1:6379> RPUSH mylist "hello2"
(integer) 3
127.0.0.1:6379> RPOPLPUSH mylist myotherlist
"hello2"
127.0.0.1:6379> LRANGE mylist 0 -1  #  Look at the original list 
1) "hello"
2) "hello1"
127.0.0.1:6379> LRANGE myotherlist 0 -1 #  Look at the target list , Values do exist  
1) "hello2"

########################################################
LSET  Replaces the value of the specified subscript in the list with another value , update operation 

127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> EXISTS list  #  First determine whether the list exists 
(integer) 0
127.0.0.1:6379> LSET list 0 item #  If there is no list to update, an error will be reported 
(error) ERR no such key
127.0.0.1:6379> LPUSH list value1
(integer) 1
127.0.0.1:6379> LRANGE list 0 0
1) "value1"
127.0.0.1:6379> LSET list 0 item #  If there is , Update the value of the current subscript 
OK
127.0.0.1:6379> LRANGE list 0 0
1) "item"
127.0.0.1:6379> LSET list 1 other #  If it doesn't exist , May be an error 
(error) ERR index out of range

########################################################
LINSERT  Put a specific value Insert before or after an element !

127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> LPUSH mylist "hello"
(integer) 1
127.0.0.1:6379> LPUSH mylist "world"
(integer) 2
127.0.0.1:6379> LINSERT mylist before world "other"
(integer) 3
127.0.0.1:6379> LRANGE mylist 0 -1
1) "other"
2) "world"
3) "hello"
127.0.0.1:6379> LINSERT mylist after world "new"
(integer) 4
127.0.0.1:6379> LRANGE mylist 0 -1
1) "other"
2) "world"
3) "new"
4) "hello"

Summary

  • He's actually a linked list ,before Node after 、left、right You can insert values
  • If key non-existent , Create a new list
  • If key There is , The new content
  • If you remove all the values , Empty list , It also means that there is no such thing as !
  • Insert or change values on both sides , Highest efficiency ! The middle element , Relatively speaking, the efficiency will be a little lower

Message queuing ! Message queue (Lpush Rpop) Left and right exit - queue Stack (Lpush Lpop) Left and left out

原网站

版权声明
本文为[Don't like learning since childhood~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011702009304.html