Summary of common commands for Redis list types

  • 2020-05-10 23:09:21
  • OfStack

Introduction to list types

The list type is also a type that we're going to be using for a long time. For example, when we blog, we use a list of blogs. If we don't have a list, we can only walk through the keys to get all the articles or part 1 of the articles. This syntax is keys, but this command needs to walk through all the keys in the database. For performance reasons, it is not recommended in production environments.

A list type can store an ordered list of strings. Common operations are to add, remove, get an element, or a fragment, to both ends of the list. In redis, it is actually implemented in the way of bidirectional linked lists, so the time complexity of adding and deleting elements at both ends of the list is O(1), and the closer the element is to both ends, the faster it will be. But accessing elements through an index can be slow, especially if the list is long. But if you just get the first element at the beginning or end of the queue, it doesn't matter how long the queue is. Therefore, the queue is very suitable for our comment function, as well as some new features such as the development of the log is also very useful.

1. Add element commands

Add elements to both ends of the list:


redis> lpush key value [value ...]
redis> rpush key value [value ...]

It's easy to see here, lpush l is left, and to the left of the list, r is right.   ( lpush , rpush Similar to the action queue, to the left push To the right, push )


C:\Program Files\Redis>redis-cli.exe
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> lpush mylist 0
(integer) 1
127.0.0.1:6379> rpush mylist 1
(integer) 2
127.0.0.1:6379> lpush mylist -1 -2
(integer) 4
127.0.0.1:6379> lrange mylist 0 -1
1) "-2"
2) "-1"
3) "0"
4) "1"

The return value is the length of the list. You can have multiple values for push here, but there's a point 1 to make. It looks like up here on the left push Minus 1, minus 2, and actually now the list is sorted like this, minus 2, minus 1, 0, 1, so it's the same time push Multiple, actually one by one insert list, but these operations are all atomic.

2. Pop up the element command


redis> lpop key
redis> rpop key

It's easy to understand here, it's going to pop up from the left, it's going to pop up from the right, and the return value is going to be the value that pops up. (pop up means remove the value and return the value.)


127.0.0.1:6379> lrange mylist 0 -1
1) "-2"
2) "-1"
3) "0"
4) "1"
127.0.0.1:6379> lpop mylist
"-2"
127.0.0.1:6379> rpop mylist
"1"
127.0.0.1:6379> lrange mylist 0 -1
1) "-1"
2) "0"

Combined with the above four commands, the stack and queue can be implemented.

The stack: lpush and lpop Or use rpush and lpush0 .

Queue: lpush and lpush0 Or use rpush and lpop .

3. Get the number of elements in the list


redis> llen key

It returns zero when the key doesn't exist, which you could have guessed if you had learned the one way. Here redis takes O(1) to execute this command, unlike O(N) in a relational database.


127.0.0.1:6379> llen mylist
(integer) 2

4. Get the list fragment


redis> lrange key start stop

lrange The more common command returns a list of all elements from start to stop, with start and stop starting at 0.


127.0.0.1:6379> lrange mylist 0 -1
1) "-1"
2) "0"

lrange It also supports a negative index, which is a negative value and you can think of it as the number from the right.

So, for example, now numbers is minus 2, minus 1, 0, 1, and we're going to execute it lrange numbers -2 -1 You get the last two values. There are lrange numbers 0 -1 Is the command we often use to get the full list.

Here are two things to note:

      (1) if the start index is lower than the stop index position (the position, not the size of the index value), an empty list is returned (empty list or set).

      (2) if stop is larger than the actual index range, the last element of the list is returned.

5. Delete the specified value in the list


redis> lrem key count value

lrem The count command deletes the first value elements in the list and returns the number of elements actually deleted. The actual count is different in size and execution.

        (1) count > 0 : delete the previous count elements with the value value from the left of the list

        (2) count < 0 : delete from the right

        (3) count = 0 : delete all

The return value is the number of deleted values.

6. Gets/sets the element value of the specified index


redis> lindex key index
redis> lset key index value

So this should make a lot of sense, special note 1 if index is negative then you're going to count the index from the right, and lrange is negative 1.


C:\Program Files\Redis>redis-cli.exe
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> lpush mylist 0
(integer) 1
127.0.0.1:6379> rpush mylist 1
(integer) 2
127.0.0.1:6379> lpush mylist -1 -2
(integer) 4
127.0.0.1:6379> lrange mylist 0 -1
1) "-2"
2) "-1"
3) "0"
4) "1"
0

7. Keep only the fragments specified in the list


C:\Program Files\Redis>redis-cli.exe
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> lpush mylist 0
(integer) 1
127.0.0.1:6379> rpush mylist 1
(integer) 2
127.0.0.1:6379> lpush mylist -1 -2
(integer) 4
127.0.0.1:6379> lrange mylist 0 -1
1) "-2"
2) "-1"
3) "0"
4) "1"
1

I'm not going to make a special statement here.


C:\Program Files\Redis>redis-cli.exe
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> lpush mylist 0
(integer) 1
127.0.0.1:6379> rpush mylist 1
(integer) 2
127.0.0.1:6379> lpush mylist -1 -2
(integer) 4
127.0.0.1:6379> lrange mylist 0 -1
1) "-2"
2) "-1"
3) "0"
4) "1"
2

Insert an element into the list

Now the values in our list are 0, negative 1, negative 2


redis> linsert key before|after pivot value

C:\Program Files\Redis>redis-cli.exe
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> lpush mylist 0
(integer) 1
127.0.0.1:6379> rpush mylist 1
(integer) 2
127.0.0.1:6379> lpush mylist -1 -2
(integer) 4
127.0.0.1:6379> lrange mylist 0 -1
1) "-2"
2) "-1"
3) "0"
4) "1"
4

Now the values in the list are 1,1,0,-1,-1.5,-2. The linsert command first looks for the value of pivot And then, depending on whether it's before or after, add elements before or after.

9. Dump elements from one list to another


C:\Program Files\Redis>redis-cli.exe
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> lpush mylist 0
(integer) 1
127.0.0.1:6379> rpush mylist 1
(integer) 2
127.0.0.1:6379> lpush mylist -1 -2
(integer) 4
127.0.0.1:6379> lrange mylist 0 -1
1) "-2"
2) "-1"
3) "0"
4) "1"
5

This command is very interesting. Execute it first lpush0 To perform lpush . This command will be followed first source One element pops up on the right to insert destination To the left of the list and returns the value of this element. The whole thing is also atomic.

conclusion

Here we list the types of commands are all introduced, is not very simple ~ hope this article for everyone's study or work can bring 1 definite help, if there is a problem you can leave a message to communicate.


Related articles: