Redis的List

A Redis list is a simple list of strings, sorted by insertion order. You can add an element to either the head (left) or the tail (right) of the list

A list can contain up to 232-1 elements (4294967295, more than 4 billion elements per list).

Since the List of Redis supports both header and tail operations, the List is actually understood as a two-way linked List, which can be used as a stack or a queue.

Application scenarios of List

Let’s assume that to get the latest login information of 10 users, in a traditional relational database, we can do the following:

select * from user order by logintime limit 10
Copy the code

We can easily complete this query operation with a SINGLE SQL statement.

But the problem is, it is assumed that the amount of data is very large, we are now database, that is, the number of users a lot, so we traverse the query time is needed for many at a time, which is slower operation, but also increase the load of the database, at the same time, if we set up the corresponding field index, then the database is also a kind of consumption of resources.

This is where NoSQL comes in. We can use the redis list type to keep only the last 10 data in the list, and every time a new data comes in, we delete an old data, so that we always maintain the last 10 users in the list.

Redis list command

Lpush command

The Redis Lpush command inserts one or more values into the list header. If the key does not exist, an empty list is created and an LPUSH operation is performed. An error is returned when the key exists but is not of list type. The syntax of the redis Lpush command is as follows:

Redis 127.0.0.1:6379> LPUSH KEY_NAME VALUE1.. VALUENCopy the code

Rpop command

The Redis Rpop command is used to remove and return the last element of the list. Syntax The syntax of the redis Rpop command is as follows:

Redis 127.0.0.1:6379 > RPOP KEY_NAMECopy the code

Return value: The last element of the list. Returns nil when the list does not exist.

Lrange command

Redis Lrange returns elements within the specified range in the list, specified by the offsets START and END. Where 0 represents the first element of the list, 1 represents the second element of the list, and so on. You can also use negative subscripts, -1 for the last element of the list, -2 for the next-to-last element of the list, and so on. Syntax The syntax of the redis Lrange command is as follows:

Redis 127.0.0.1:6379> LRANGE KEY_NAME START ENDCopy the code

Returns a list of elements within a specified range.

ltrim

Redis Ltrim Trims a list. That is, the list is trimmed so that only elements within a specified range are left in the list. Elements outside the specified range are deleted. The subscript 0 indicates the first element of the list, the subscript 1 indicates the second element of the list, and so on. You can also use negative subscripts, -1 for the last element of the list, -2 for the next-to-last element of the list, and so on. Syntax The syntax of the redis Ltrim command is as follows:

Redis 127.0.0.1:6379> LTRIM KEY_NAME START STOPCopy the code

Return value Ok is returned when the command is successfully executed.

Lrem command

Redis Lrem removes elements in the list that are equal to the VALUE of the parameter COUNT. The value of COUNT can be one of the following:

  • Count > 0: Searches from the top of the table to the end of the table and removes elements equal to VALUE.
  • Count < 0: Searches from the end of the table to the top, removing elements equal to VALUE, the absolute VALUE of count.
  • Count = 0: Removes all values equal to VALUE from the table.

Syntax The syntax of the redis Lrem command is as follows:

Redis 127.0.0.1:6379> LREM KEY_NAME COUNT VALUECopy the code

Returns the number of elements removed. Returns 0 if the list does not exist.

Linsert command

The Redis Linsert command is used to insert an element before or after an element in a list. When the specified element does not exist in the list, no action is performed. When the list does not exist, it is treated as an empty list and no action is performed. If the key is not a list type, an error is returned. Syntax The syntax of the redis Linsert command is as follows:

Redis 127.0.0.1:6379> LINSERT KEY_NAME BEFORE EXISTING_VALUE NEW_VALUECopy the code

Return value If the command executed successfully, returns the length of the list after the insertion. If the specified element is not found, -1 is returned. If key does not exist or is an empty list, 0 is returned.

Lset command

Redis Lset sets the value of an element by index. An error is returned when an index parameter is out of range, or when LSET is performed on an empty list. For more information about list subscripts, see the LINDEX command. Syntax The syntax of the redis Lset command is as follows:

Redis 127.0.0.1:6379> LSET KEY_NAME INDEX VALUECopy the code

Return value Operation returns OK on success, error message otherwise.