##Redis data type List operation
In redis , you can cleverly use lists into stacks, queues, blocking queues, etc.
1. Push insert elements
1. lpush, insert at the head
Insert one value or multiple values into the head of the list.
lpush list onelpush list twolpush list three
Copy after login
Use
lpush, the l here can be regarded as left, that is, inserted on the left, so the current list is like this
[three , two, one].
2. rpush, insert
Next, use
rpush to insert elements on the right, that is, at the end of the list.
rpush list right1 right2
Copy after login
At this time, the list is like this
[three, two, one, right1, right2]. Use
lrange list 0 -1 to get:
2. Range Get elements through intervals
can be imagined as in python
range(), get the element by passing in the start and end subscripts.
lrange list 1 4
Copy after login
3. Pop removal element
Since the above can be added to the left and right, then the removal can naturally be divided into left and right.
Removal operation returns the removed element.
1. lpop removes the left side
lpop list
Copy after login
2. rpop removes the right side
rpop list
Copy after login
4. lindex gets the element through the subscript
lindex list 1
Copy after login
5. llen gets the list length
Returns the length of the list.
llen list
Copy after login
6. lrem removes specified elements
You can specify the element to be removed, and specify the number.
lrem list 2 yi222
Copy after login
There are 3 yi222s in the list now, and I want to remove 2 of them.
7. ltrim trimming
Through
ltrim, only the specified part is retained, and other parts are removed, and the intercepted list changes. .
ltrim list 1 4
Copy after login
Here the subscripts from 1 to 4 are retained, and the others are removed.
8. Combination command rpoplpush remove and add
This is a combination command, remove the last element of the list and add it to another list .
rpoplpush list list2
Copy after login
Here
list is the original list,
list2 is the target list, and if the target list does not exist, it will be created.
9. exists Determine whether the key exists
exists list
Copy after login
Returns 1 if it exists, and returns 0 if it does not exist.
10. lset, set the value of the specified subscript
lset list 1 test
Copy after login
When the index parameter is out of range, or LSET is performed on an empty list, an error is returned.
11. linsert, insert the value before/after the specified position
before
linsert list before test before_test
Copy after login
This is the element
testBefore, insert element
before_test.
After
linsert list after test after_test
Copy after login
This is after element
test, insert element
after_test.
The above is the detailed content of What are the common operation commands of Redis basic data type List?. For more information, please follow other related articles on the PHP Chinese website!