Lists types and operations of Redis
List is a linked list structure, the main function is push, pop, get all the values of a range and so on. Key in operation is understood as the name of the linked list. The Redis list type is actually a doubly linked list in which each child element is a string type. You can add delete elements from the head or tail of the list by push, pop operations, so that the list can be either a stack or a queue.
Stack and queue characteristics stack: Advanced back out; queue: FIFO first.
1. Lpush adds a string element to the head of the linked list
Format: Lpush mylist1 "Allen"
Meaning: In linked list mylist1, add an element from the head Allen
2. Lrange list of elements in a linked list
Format: Lrange mylist1 0-1
Meaning: Lists the linked list Mylist1, from the first to the last element. 0, which represents the first element from the beginning. -1, which means the first element of the tail, the whole meaning is to take from the beginning to the tail.
3. Rpush the element into the rear
Format: Rpush mylist1 "Age"
Meaning: For list mylist1, press an element from the tail to age.
PS: Remember that the direction of the head is the front.
4. LSet sets the element value for the subscript specified in the list.
Format: LSet mylist1 0 "Frank"
Meaning: For the linked list Mylist1, the subscript is 0 element, the value is changed to Frank.
Note: The subscript is started from scratch. Successful return OK, subscript does not exist: (error) ERR index out of range
5. Lrem removes n and the same elements from the list as the given values.
Format: Lrem mylist1 2 Allen
Meaning: From the list mylist1, delete the 2 elements that are the value of Allen.
Note: It is removed from the head in turn. Delete success: Returns the number of deleted, delete failed to return 0.
Note: If n is less than 0, it is deleted from the tail; n=0, which means delete all.
6. LTrim retains data within the value range of the specified key.
Format: LTrim mylist1 1 2
Meaning: Keep the list mylist1 all elements between subscript 1 and subscript 2, and the elements outside this range are deleted.
Note: This is the data that is retained and is not deleted. Delete successfully returned OK. If the subscript does not exist, the entire list will be emptied.
7. Lpop pops an element from the head
Format: Lpop mylist1
Meaning: POPs an element from the head of the linked list mylist1. The return value is the value of the popup element.
8. Rpop pops an element from the tail
Format: Rpop mylist1
Meaning: An element pops up from the tail of the linked list mylist1. The return value is the value of the popup element.
9. Rpoplpush pops an element from the tail of the first list and adds it to the head of the second list.
Format: Rpoplpush mylist1 mylist2
Meaning: pops an element from the tail of the linked list Mylist1 and adds it to the head of the second linked list mylist2.
Example:
127.0.0.1:6379> Lrange Mylist1 0-1
1) "Age"
2) "Allen"
127.0.0.1:6379> Lrange Mylist2 0-1
1) "Sex"
127.0.0.1:6379> Rpoplpush Mylist1 Mylist2
"Allen"
127.0.0.1:6379> Lrange Mylist1 0-1
1) "Age"
127.0.0.1:6379> Lrange Mylist2 0-1
1) "Allen"
2) "Sex"
As you can tell by example, the return value is the value that pops up.
LINDEX Specifies the subscript and returns the corresponding element.
Format: Lindex mylist2 2
Meaning: Returns the element labeled 2 in the List 2.
Llen returns the number of elements in the linked list.
Format: Llen mylist2
Meaning: View the number of elements in the linked list mylist2.
learn PHP's small ant original blog http://my.oschina.net/woshixiaomayi/blog
Small Ant learns Redis notes (5)--redis data Type list type