Detailed explanation of list types and related commands in redis
1. Overview:
In Redis, the List type is a linked list of strings sorted in insertion order. Like an ordinary linked list in a data structure, we can add new elements to its head (left) and tail (right). During insertion, if the key does not exist, Redis will create a new linked list for the key. (Recommended: redis video tutorial)
On the contrary, if all elements in the linked list are removed, then the key will also be deleted from the database. The maximum number of elements that can be contained in a List is 4294967295.
From the efficiency perspective of element insertion and deletion, if we insert or delete elements at both ends of the linked list, this will be a very efficient operation, even if millions of records have been stored in the linked list. Operations can also be completed in constant time.
However, it should be noted that if the element insertion or deletion operation is performed in the middle of the linked list, it will be very inefficient. I believe this is not difficult to understand for developers with a good data structure foundation.
2. Related command list:
Command prototype | Time complexity | Command description | Return value |
LPUSH key value [value ...] | O(1) | Insert all the Values given in the parameters into the header of the List Value associated with the specified Key. If the Key does not exist, this command will create an empty linked list associated with the Key before inserting, and then insert the data from the head of the linked list. If the value of the key is not of linked list type, this command will return relevant error information. | The number of elements in the linked list after insertion. |
LPUSHX key value | O(1) | Only when the Key specified in the parameter exists, the command will be associated with it The head of the List Value is inserted into the Value given in the parameter, otherwise no operation will occur. | The number of elements in the linked list after insertion. |
LRANGE key start stop | O(S N) | The S in time complexity is the offset represented by the start parameter, and N represents the element quantity. The parameters start and end of this command are both 0-based. That is, 0 represents the first element at the head (leftmost) of the linked list. The value of start can also be a negative value, -1 will represent the last element in the linked list, that is, the tail element, -2 will represent the second to last and so on. When this command obtains elements, the elements at the start and end positions will also be taken out. If the value of start is greater than the number of elements in the linked list, an empty linked list will be returned. If the value of end is greater than the number of elements, this command obtains all remaining elements in the linked list starting from start (including start). | Returns a list of elements within the specified range. |
LPOP key | O(1) | Return and pop up the first element in the linked list associated with the specified Key, that is, the head element. . If the Key does not exist, return nil. | The element at the head of the linked list. |
LLEN key | O(1) | Returns the number of elements in the linked list associated with the specified Key. If the Key does not exist, returns 0 . If the type of Value associated with the Key is not a linked list, relevant error information is returned. | The number of elements in the linked list. |
LREM key count value | O(N) | N in time complexity represents the number of elements in the linked list. In the linked list associated with the specified Key, delete the first count elements whose value is equal to value. If count is greater than 0, traverse from beginning to end and delete. If count is less than 0, traverse from end to beginning and delete. If count is equal to 0, delete all elements in the linked list that are equal to value. If the specified Key does not exist, 0 is returned directly. | Returns the number of deleted elements. |
LSET key index value | O(N) | N in time complexity represents the number of elements in the linked list. But when setting the head or tail elements, the time complexity is O(1). Set the value of the specified position in the linked list to the new value, where 0 represents the first element, that is, the head element, and -1 represents the tail element. If the index value Index exceeds the number of elements in the linked list, this command will return relevant error information. | |
LINDEX key index | O(N) | N in time complexity means that it needs to be traversed when finding the element the number of elements. For head or tail elements, its time complexity is O(1). This command will return the element at the specified position (index) in the linked list. The index is 0-based, which means the head element. If the index is -1, it means the tail element. If the Key is not associated with a linked list, this command will return relevant error information. | Return the requested element, or nil if the index is out of range. |
LTRIM key start stop | O(N) | N represents the number of deleted elements. This command will only retain elements within the specified range, thus keeping the number of elements in the link relatively constant. The start and stop parameters are both 0-based, with 0 indicating the header element. Like other commands, start and stop can also be negative values, and -1 represents the tail element. If start is greater than the end of the linked list, or start is greater than stop, this command will not report an error, but will return an empty linked list, and the Key will also be deleted. If stop is greater than the number of elements, all elements remaining from start are retained. | |
LINSERT key BEFORE|AFTER pivot value | O(N) | In time complexity, N means that when finding the The number of elements that need to be traversed before the element pivots. This means that if the pivot is at the head or tail of the linked list, the time complexity of this command is O(1). The function of this command is to insert the element value in the parameter before or after the pivot element. If the Key does not exist, this command will do nothing. If the Value type associated with Key is not a linked list, related error information will be returned. | The number of elements in the linked list after successful insertion. If pivot is not found, -1 is returned. If key does not exist, 0 is returned. |
RPUSH key value [value ...] | O(1) | Insert the parameter at the end of the List Value associated with the specified Key All Values given. If the Key does not exist, this command will create an empty linked list associated with the Key before inserting, and then insert the data from the end of the linked list. If the value of the key is not of linked list type, this command will return relevant error information. | The number of elements in the linked list after insertion. |
RPUSHX key value | O(1) | Only when the Key specified in the parameter exists, the command will be associated with it The value given in the parameter is inserted at the end of the List Value, otherwise no operation will occur. | The number of elements in the linked list after insertion. |
O(1) | Return and pop up the last element in the linked list associated with the specified Key, that is, the tail element. If the Key does not exist, return nil. | The element at the end of the linked list. | |
O(1) | Atomicly pop an element from the end of the linked list associated with the source key, and then pop it up again The element is inserted into the head of the linked list associated with the destination key. If the source key does not exist, the command will return nil and no other operations will be performed. If source and destination are the same key, it is equivalent to atomically moving the tail element in its associated linked list to the head of the linked list. | Returns popped and inserted elements. |
The above is the detailed content of Detailed explanation of list types and related commands in redis. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

There are two types of Redis data expiration strategies: periodic deletion: periodic scan to delete the expired key, which can be set through expired-time-cap-remove-count and expired-time-cap-remove-delay parameters. Lazy Deletion: Check for deletion expired keys only when keys are read or written. They can be set through lazyfree-lazy-eviction, lazyfree-lazy-expire, lazyfree-lazy-user-del parameters.
