Redis commands: list


Redis list(List)

Redis list (List) is one of the Redis data structures. It is an ordered list of strings that can be accessed by index. Each element in the list is unique, and queue (FIFO) or stack (LIFO) operations can be implemented by inserting or deleting elements from both ends of the list.

Redis lists are very useful in practical applications, for example, they can be used to implement functions such as message queues, logging, and rankings. Here are some common commands for Redis lists:

  • LPUSH key value: Insert one or more values ​​into the head of the list.
  • RPUSH key value: Insert one or more values ​​to the end of the list.
  • LPOP key: Removes and returns the element at the head of the list.
  • RPOP key: Remove and return the element at the end of the list.
  • LLEN key: Returns the length of the list.
  • LRANGE key start stop: Returns elements within the specified range in the list.
  • LINDEX key index: Returns the element at the specified index position in the list.
  • LSET key index value: Sets the element at the specified index position in the list to a new value.
  • LREM key count value: Removes the specified number of specified elements from the list.

By using these commands, you can easily create, operate, and manage list data structures in Redis to meet the needs of various practical applications.

Example

Here are some examples of Redis list commands:

  1. LPUSH command: Inserts one or more values ​​into the head of the list.
LPUSH mylist "Hello"
LPUSH mylist "World"
  1. RPUSH command: Inserts one or more values ​​to the end of the list.
RPUSH mylist "Hello"
RPUSH mylist "World"
  1. LPOP command: removes and returns the element at the head of the list.
LPOP mylist
  1. RPOP command: removes and returns the element at the end of the list.
RPOP mylist
  1. LLEN command: Returns the length of the list.
LLEN mylist
  1. LRANGE command: Returns the elements in the specified range in the list.
LRANGE mylist 0 -1  # 返回整个列表
LRANGE mylist 0 1   # 返回列表中的前两个元素
  1. LINDEX command: Returns the element at the specified index position in the list.
LINDEX mylist 0  # 返回列表中的第一个元素
LINDEX mylist 1  # 返回列表中的第二个元素
  1. LSET command: Sets the element at the specified index position in the list to a new value.
LSET mylist 0 "New Value"  # 将列表中的第一个元素设置为新值
  1. LREM command: Removes a specified number of specified elements from a list.
LREM mylist 2 "Hello"  # 从列表中移除前两个值为 "Hello" 的元素

These commands can help you create, operate, and manage list data structures in Redis to meet various practical application requirements, such as message queues, logging, and rankings. Please note that only some common command examples are provided here. Redis also provides other more advanced list operation commands and functions.

Redis list(List)

The following is a table of basic commands related to Redis lists:

Order describe
LPUSH key value Inserts one or more values ​​into the head of the list.
RPUSH key value Inserts one or more values ​​into the end of the list.
LPOP key Removes and returns the element at the head of the list.
RPOP key Removes and returns the element at the end of the list.
LLEN key Returns the length of the list.
LRANGE key start stop Returns the elements in the specified range in the list.
LINDEX key index Returns the element at the specified index in the list.
LSET key index value Sets the element at the specified index in the list to a new value.
LREM key count value Removes the specified number of specified elements from the list.
LINSERT key BEFORE/AFTER pivot value Inserts a new element before or after the specified element in the list.
RPOPLPUSH source destination Removes the last element from a list and adds that element to another list and returns.
BRPOPLPUSH source destination timeout Removes the last element from a list, adds it to another list and returns, setting a timeout.
BLPOP key1 key2 timeout Pops (removes and obtains) the leftmost element from one or more lists. If the list has no elements, it blocks the list until the wait times out or a popable element is found.
BRPOP key1 key2 timeout Pops (removes and obtains) the rightmost element from one or more lists. If the list has no elements, it blocks the list until the wait times out or a popable element is found.
LTRIM key start stop Trim a list to only keep elements within a specified range.

Guess you like

Origin blog.csdn.net/m0_62617719/article/details/133140773