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"String Similar scenario: value can be our number in addition to our string! - 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!
Redis: Improving Application Performance and ScalabilityApr 17, 2025 am 12:16 AMRedis improves application performance and scalability by caching data, implementing distributed locking and data persistence. 1) Cache data: Use Redis to cache frequently accessed data to improve data access speed. 2) Distributed lock: Use Redis to implement distributed locks to ensure the security of operation in a distributed environment. 3) Data persistence: Ensure data security through RDB and AOF mechanisms to prevent data loss.
Redis: Exploring Its Data Model and StructureApr 16, 2025 am 12:09 AMRedis's data model and structure include five main types: 1. String: used to store text or binary data, and supports atomic operations. 2. List: Ordered elements collection, suitable for queues and stacks. 3. Set: Unordered unique elements set, supporting set operation. 4. Ordered Set (SortedSet): A unique set of elements with scores, suitable for rankings. 5. Hash table (Hash): a collection of key-value pairs, suitable for storing objects.
Redis: Classifying Its Database ApproachApr 15, 2025 am 12:06 AMRedis's database methods include in-memory databases and key-value storage. 1) Redis stores data in memory, and reads and writes fast. 2) It uses key-value pairs to store data, supports complex data structures such as lists, collections, hash tables and ordered collections, suitable for caches and NoSQL databases.
Why Use Redis? Benefits and AdvantagesApr 14, 2025 am 12:07 AMRedis is a powerful database solution because it provides fast performance, rich data structures, high availability and scalability, persistence capabilities, and a wide range of ecosystem support. 1) Extremely fast performance: Redis's data is stored in memory and has extremely fast read and write speeds, suitable for high concurrency and low latency applications. 2) Rich data structure: supports multiple data types, such as lists, collections, etc., which are suitable for a variety of scenarios. 3) High availability and scalability: supports master-slave replication and cluster mode to achieve high availability and horizontal scalability. 4) Persistence and data security: Data persistence is achieved through RDB and AOF to ensure data integrity and reliability. 5) Wide ecosystem and community support: with a huge ecosystem and active community,
Understanding NoSQL: Key Features of RedisApr 13, 2025 am 12:17 AMKey features of Redis include speed, flexibility and rich data structure support. 1) Speed: Redis is an in-memory database, and read and write operations are almost instantaneous, suitable for cache and session management. 2) Flexibility: Supports multiple data structures, such as strings, lists, collections, etc., which are suitable for complex data processing. 3) Data structure support: provides strings, lists, collections, hash tables, etc., which are suitable for different business needs.
Redis: Identifying Its Primary FunctionApr 12, 2025 am 12:01 AMThe core function of Redis is a high-performance in-memory data storage and processing system. 1) High-speed data access: Redis stores data in memory and provides microsecond-level read and write speed. 2) Rich data structure: supports strings, lists, collections, etc., and adapts to a variety of application scenarios. 3) Persistence: Persist data to disk through RDB and AOF. 4) Publish subscription: Can be used in message queues or real-time communication systems.
Redis: A Guide to Popular Data StructuresApr 11, 2025 am 12:04 AMRedis supports a variety of data structures, including: 1. String, suitable for storing single-value data; 2. List, suitable for queues and stacks; 3. Set, used for storing non-duplicate data; 4. Ordered Set, suitable for ranking lists and priority queues; 5. Hash table, suitable for storing object or structured data.
How to implement redis counterApr 10, 2025 pm 10:21 PMRedis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment







