List common operations of redis data type


Redis's list list type 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. A list can contain up to 232 - 1 elements (4294967295, over 4 billion elements per list).


lpush creates a list and adds elements (lpush key value1 [value2] inserts one or more values ​​into the head of the list)

redis 127.0.0.1:6379> lpush regions xian
(integer) 1
redis 127.0.0.1:6379> lpush regions shanghai
( integer) 2
redis 127.0.0.1:6379> lpush regions beijing
(integer) 3
redis 127.0.0.1:6379> lpush regions guangzhou
(integer) 4

lrange get all elements
redis 127.0.0.1:6379> lrange regions 0 4 //get All elements
1) "guangzhou"
2) "beijing"
3) "shanghai"
4) "xian"


Lrange Get the specified range of elements
redis 127.0.0.1:6379>
1) "shanghai"
2) "xian"


llen Get the length of the list
redis 127.0.0.1:6379> llen regions
(integer) 4


lindex Get a subscript element
redis 127.0.0.1:6379> lindex regions 1
"beijing" Lpop shift Divide and return the first element of the list redis 127.0.0.1:6379> lpop regions        "guangzhou" //Note that guangzhgou in the list is the first element that has been removed redis 127.0.0.1:6379> lrange regions 0 4 / /look at the list again and sure enough the first element before was removed 1) "beijing" 2) "shanghai" 3) "xian" lpushx inserts one or more elements into the head of the list redis 127.0.0.1:6379> lpushx regions guangzhou //We insert the previously removed guangzhou back (integer) 4 redis 127.0.0.1:6379> lrange regions 0 4 //Note 1) "guangzhou" is inserted in the header















1) "guangzhou"
2) "beijing"
3) "shanghai"
4) "xian" rpop remove and return the last element of the list redis 127.0.0.1:6379> rpop regions //remove and return the last element "xian" " redis 127.0.0.1:6379> lrange regions 0 4 //Check again Note: The last element xian is removed 1) "guangzhou" 2) "beijing" 3) "shanghai" Lset sets an element of the list by index The value of redis 127.0.0.1:6379> lset regions 1 chongqing //Set the element whose index is 1 to chongqing OK redis 127.0.0.1:6379> lrange regions 0 3 //Check again 1) "guangzhou" 2) "chongqing " //Note: here was beijing now it has become chongqing                                    3) " shanghai" rpush adds one or more values ​​to a list



















redis 127.0.0.1:6379> rpush regions beijing
(integer) 4

rpush add value to existing list
redis 127.0.0.1:6379> rpush regions hunan
(integer) 5

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326750473&siteId=291194637