Analysis of String data type examples in Redis
Overview:
The string type is the most basic data storage type in Redis. It is binary safe in Redis, which means that this type can accept data in any format, such as JPEG image data. Or Json object description information, etc. The maximum capacity of string type values in Redis is 512MB.
Related command list:
Command prototype | Time complexity | Command description | Return value |
##APPEND | O(1)If the Key already exists, the APPEND command appends the data of the parameter Value to the end of the existing Value. If the Key does not exist, the APPEND command will create a new Key/Value. | The length of Value after appending. | |
DECR | O(1)Decrement the Value of the specified Key atomically by 1. If the Key does not exist, its initial value is 0 and its value after decr is -1. If the value of Value is not of integer type, such as Hello, the operation will fail and return the corresponding error message. Note: The value range of this operation is a 64-bit signed integer. | Decreasing Value.||
INCR | O(1)Atomicly increment the Value of the specified Key by 1. If the Key does not exist, its initial value is 0 and its value after incr is 1. If the value of Value cannot be converted to an integer value, such as the string Hello, then the operation will fail and return the corresponding error message. Note: The value range of this operation is a 64-bit signed integer. | The incremented Value value.||
DECRBY | O(1)The Value of the specified Key will be atomically reduced by decrement. After decrby is executed, if the Key does not exist, its initial value is 0, and then its value becomes -decrement. If the value of Value cannot be converted to an integer value, such as Hello, the operation will fail and the corresponding error message will be returned. Note: The value range of this operation is a 64-bit signed integer. | The reduced Value value.||
INCRBY | O(1)Increments the Value of the specified Key atomically. If the Key does not exist, its initial value is 0, and its value after incrby is increment. If the value of Value cannot be converted to an integer value, such as Hello, the operation will fail and the corresponding error message will be returned. Note: The value range of this operation is a 64-bit signed integer. | The increased Value value.||
GET | O(1)Get the Value of the specified Key. If the Value associated with the Key is not of string type, Redis will return an error message because the GET command can only be used to obtain string Value. | Value related to the Key. If the Key does not exist, nil is returned. | |
SET | O(1)Set the Key to hold the specified string Value. If the Key If it already exists, its original value will be overwritten. | Always returns "OK". | |
GETSET | O(1)Atomicly set the Key to the specified Value and return the Key at the same time the original value. Like the GET command, this command can only process string Value, otherwise Redis will give relevant error information. | Return the original value of the Key. If the Key does not exist before, return nil. | |
STRLEN | O(1)Returns the character value length of the specified Key. If Value is not of string type, Redis The execution will fail and relevant error information will be given. | Returns the Value character length of the specified Key. If the Key does not exist, returns 0. | |
SETEX | O(1)Complete two operations atomically. One is to set the value of the Key to Specify a string and set the survival time (seconds) of the Key in the Redis server. This command is mainly used when Redis is used as a Cache server. | ||
SETNX | O(1)If the specified Key does not exist, set The Key is determined to hold the specified string Value. At this time, its effect is equivalent to the SET command. On the contrary, if the Key already exists, the command will do nothing and return. | 1 means the setting is successful, otherwise 0. | |
SETRANGE | O(1)Replace part of the string value of the specified Key. Starting from offset, the replacement length is the string length of the third parameter value of the command. If the value of offset is greater than the string length of the original value of the Key, Redis will be in the string length of Value. Then pad the (offset - strlen(value)) number of 0x00, and then append the new value. If the key does not exist, this command will assume that its original value length is 0, and append offset characters 0x00 before appending the new value. Given that the maximum length of the string Value is 512M, the maximum value of offset is 536870911. The last thing to note is that if the command causes the original value of the specified Key to be lost when executed, when the length increases, Redis will reallocate enough memory to accommodate all the replaced strings, so it will be affected to a certain extent. affect performance. | The modified string Value length. | |
GETRANGE | O(1) |
If the length of the intercepted string is very short, we can The time complexity of the command is considered O(1), otherwise it is O(N), where N represents the length of the intercepted substring. When this command intercepts a substring, it will include both start (0 represents the first character) and end characters in a closed interval. If the end value exceeds the character length of Value, this command will Will just intercept all character data starting from start. Substring |
|
O(1) | Set the BIT value on the specified Offset. This value can only be 1 or 0. After setting, this command returns the original BIT value on the Offset. If the specified Key does not exist, this command will create a new value and set the BIT value in the parameter at the specified |
Offset. If Offset is greater than the character length of Value, Redis will stretch the Value and set the BIT value in the parameter on the specified Offset, with the BIT value added in the middle being 0. The last thing that needs to be is that the Offset value must be greater than 0. The original value of the BIT at the specified Offset. |
|
O(1) | Returns the value of the BIT at the specified Offset, 0 or 1. This command will return 0 if Offset exceeds the length of the string value, so it always returns 0 for an empty string. | The BIT value at the specified Offset. | |
O(N) | N represents the number of keys obtained. Returns the Values of all specified Keys. If one of the Keys does not exist or its value is not of string type, the Value of the Key will return nil. | Returns a list of Values for a set of specified Keys. | |
O(N) | N represents the number of specified Keys. This command atomically completes all key/value setting operations in the parameters. Its specific behavior can be seen as executing the SET command iteratively multiple times. | This command will not fail and always returns OK. | |
O(N) | N represents the number of specified Keys. This command atomically completes all key/value setting operations in the parameters. Its specific behavior can be seen as executing the SETNX command iteratively multiple times. However, what needs to be clearly stated here is that |
If any Key in this batch of Keys already exists, then the operation will all be rolled back, that is, all modifications will not take effect. |
append key value Add value to the original value, if If the key does not exist, create a new key and value instead of appending
substr key start len intercepts the key, starting from start, intercepts the length of len
strlen key obtains the length of the key
incr key increases by 1
decr key decreases by 1
incrby key num increases key, increases num
decrby key num decreases key, decreases num
getrange key start end intercepts the character key [start, end] and the header also contains the tail
setrange key offset value replaces the data at the offset position with value (offset is the index of the key)
setex key seconds value sets the expiration time of the key
If the key does not exist, executing the setnx command will set the key and value; but if the key already exists, the setnx command will fail and the value cannot be added again
mset key1 value key2 value Set multiple keys and values at one time
mget key1 key2 Get the value of multiple keys at one time
msetnx key1 value key2 value Set multiple keys and values at one time value If one of the keys exists, all creations will fail (atomicity)
getset key value If it does not exist, get nil, and then set the value. If it refers to the value before getting it, set the subsequent value (update operation) )
############################################### 127.0.0.1:6379> set key1 v OK 127.0.0.1:6379> get key1 "v" 127.0.0.1:6379> keys * 1) "key1" 127.0.0.1:6379> exists key1 (integer) 1 127.0.0.1:6379> append key1 v1 (integer) 3 127.0.0.1:6379> get key1 "vv1" 127.0.0.1:6379> substr key1 0 3 "vv1" 127.0.0.1:6379> strlen key1 (integer) 3 127.0.0.1:6379> append key1 "hello1" (integer) 9 127.0.0.1:6379> substr key1 1 2 "v1" 127.0.0.1:6379> substr key1 1 1 "v" 127.0.0.1:6379> append key2 "lisi" (integer) 4 127.0.0.1:6379> get key2 "lisi" ############################################### 127.0.0.1:6379> set views 0 OK 127.0.0.1:6379> get views "0" 127.0.0.1:6379> incr views (integer) 1 127.0.0.1:6379> incr views (integer) 2 127.0.0.1:6379> get views "2" 127.0.0.1:6379> decr views (integer) 1 127.0.0.1:6379> decr views (integer) 0 127.0.0.1:6379> incrby views 10 (integer) 10 127.0.0.1:6379> decrby views 5 (integer) 5 #################################### 127.0.0.1:6379> set key1 "hello,world" OK 127.0.0.1:6379> GETRANGE key1 0 3 "hell" 127.0.0.1:6379> GETRANGE key1 0 -1 "hello,world" 127.0.0.1:6379> SETRANGE key2 3 2 (integer) 7 127.0.0.1:6379> get key2 "abc2efg" ######################################## 127.0.0.1:6379> setex key3 30 hello OK 127.0.0.1:6379> get key3 "hello" 127.0.0.1:6379> ttl key3 (integer) 24 127.0.0.1:6379> SETNX mykey redis (integer) 1 127.0.0.1:6379> keys * 1) "mykey" 2) "key2" 3) "key1" 127.0.0.1:6379> SETNX mykey "MongoDB" (integer) 0 127.0.0.1:6379> get mykey "redis" #################################### 127.0.0.1:6379> mset k1 v1 k2 v2 OK 127.0.0.1:6379> mget k1 k2 1) "v1" 2) "v2" 127.0.0.1:6379> MSETNX k1 v1 k3 v3 (integer) 0 # 对象 # 这里的key是一个巧妙的设计 user:{id}:{filed} 127.0.0.1:6379> msetnx user:1:name "zhangsan" user:1:age 2 (integer) 1 127.0.0.1:6379> mget user:1:name user:1:age 1) "zhangsan" 2) "2" 127.0.0.1:6379> set article:101:views 0 OK 127.0.0.1:6379> incr article:101:views (integer) 1 127.0.0.1:6379> get article:101:views "1" ########################################################### 127.0.0.1:6379> getset db redis (nil) 127.0.0.1:6379> get db "redis" 127.0.0.1:6379> getset db 10 "redis"
- Counter
- Count the number of multiple units uid:1923:follow 0
- Number of fans
- Object cache storage
The above is the detailed content of Analysis of String data type examples 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).

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.

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)

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.

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information
