Redis tutorial (2): String data type
1. 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. In Redis, the maximum data length that the string type Value can hold is 512M.
2. Related command list:
Command prototype | Time complexity | Command description | Return value |
APPENDkeyvalue | 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 the appended Value |
DECRkey | O(1 ) | Decrement the Value of the specified Key by 1 atomically. If the Key does not exist, its initial value is 0 and its value after decr is -1. 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. | Decreasing value of Value. |
INCRkey | 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 Hello, the operation will fail and the corresponding error message will be returned. Note: The value range of this operation is the incremented Value value of the 64-bit signed integer type | . |
DECRBYkey decrement | O(1) | The Value of the specified Key will be atomically reduced by decrement. If the Key does not exist, its initial value is 0, and its value is -decrement after decrby. 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. | Reduced Value |
INCRBYkey increment | O(1) | Increase the Value atomicity of the specified Key increment. 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. | Increased Value |
GETkey | 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, return nil |
SETkey value | O(1) | Set the Key to hold the specified string Value. If the Key already exists, overwrite its original value. | Always returns "OK". |
GETSETkey value | O(1) | Atomicly set the Key to the specified Value and return the original value of the Key. 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. |
STRLENkey | O(1) | Returns the character value length of the specified Key. If Value is not of string type, Redis will fail to execute and give Related error messages. | Returns the Value character length of the specified Key. If the Key does not exist, returns 0 |
SETEXkey seconds value | O(1) | Complete two operations atomically. One is to set the value of the Key to the specified string, and at the same time 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. | |
SETNXkey value | O(1) | If the specified Key does not exist, set the Key to hold the specified string Value. This 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 |
SETRANGEkey offset value | O(1) | Replace some characters of the specified Key string value. 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 fill in the end of Value (offset - strlen(value)) number of 0x00, and then append the new value. If the key does not exist, this command will assume that the length of its original value is 0, and append offset 0x00s before appending the new value. Given that the maximum length of the string Value is 512M, the maximum value of offset is 536870911. Finally, it should be noted that if the command causes the original value length of the specified Key to increase during execution, this will cause Redis to reallocate enough memory to accommodate all the replaced strings, thus causing a certain performance penalty. damage. | The modified string Value length. |
GETRANGEkey start end | O(1) | If the length of the intercepted string is very short, we can regard the time complexity of the command as 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 the character at end in a closed interval. If the end value exceeds the character length of Value, this command will only intercept the characters starting from start. All character data. | Substring |
SETBITkey offset value | O(1) | Sets the value of the BIT at the specified Offset. The value can only be 1 or 0. After setting, this command returns the original BIT value of 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 to note is that the Offset value must be greater than 0. | The original value of the BIT at the specified Offset. |
GETBITkey 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 |
MGETkey [key ...] | O(N) | N represents Get the number of Keys. 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. |
MSETkey value [key value ...] | 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. |
MSETNXkey value [key value ...] | 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. | 1 means that all Keys are set successfully, 0 means that no Key has been modified. |
3. Command examples:
1. SET/GET/APPEND/STRLEN:
/> redis-cli #执行Redis客户端工具。 redis 127.0.0.1:6379> exists mykey #判断该键是否存在,存在返回1,否则返回0。 (integer) 0 redis 127.0.0.1:6379> append mykey "hello" #该键并不存在,因此append命令返回当前Value的长度。 (integer) 5 redis 127.0.0.1:6379> append mykey " world" #该键已经存在,因此返回追加后Value的长度。 (integer) 11 redis 127.0.0.1:6379> get mykey #通过get命令获取该键,以判断append的结果。 "hello world" redis 127.0.0.1:6379> set mykey "this is a test" #通过set命令为键设置新值,并覆盖原有值。 OK redis 127.0.0.1:6379> get mykey "this is a test" redis 127.0.0.1:6379> strlen mykey #获取指定Key的字符长度,等效于C库中strlen函数。 (integer) 14
redis 127.0.0.1:6379> set mykey 20 #设置Key的值为20 OK redis 127.0.0.1:6379> incr mykey #该Key的值递增1 (integer) 21 redis 127.0.0.1:6379> decr mykey #该Key的值递减1 (integer) 20 redis 127.0.0.1:6379> del mykey #删除已有键。 (integer) 1 redis 127.0.0.1:6379> decr mykey #对空值执行递减操作,其原值被设定为0,递减后的值为-1 (integer) -1 redis 127.0.0.1:6379> del mykey (integer) 1 redis 127.0.0.1:6379> incr mykey #对空值执行递增操作,其原值被设定为0,递增后的值为1 (integer) 1 redis 127.0.0.1:6379> set mykey hello #将该键的Value设置为不能转换为整型的普通字符串。 OK redis 127.0.0.1:6379> incr mykey #在该键上再次执行递增操作时,Redis将报告错误信息。 (error) ERR value is not an integer or out of range redis 127.0.0.1:6379> set mykey 10 OK redis 127.0.0.1:6379> decrby mykey 5 (integer) 5 redis 127.0.0.1:6379> incrby mykey 10 (integer) 15
3. GETSET:
redis 127.0.0.1:6379> incr mycounter #将计数器的值原子性的递增1 (integer) 1 #在获取计数器原有值的同时,并将其设置为新值,这两个操作原子性的同时完成。 redis 127.0.0.1:6379> getset mycounter 0 "1" redis 127.0.0.1:6379> get mycounter #查看设置后的结果。 "0"
4. SETEX:
redis 127.0.0.1:6379> setex mykey 10 "hello" #设置指定Key的过期时间为10秒。 OK #通过ttl命令查看一下指定Key的剩余存活时间(秒数),0表示已经过期,-1表示永不过期。 redis 127.0.0.1:6379> ttl mykey (integer) 4 redis 127.0.0.1:6379> get mykey #在该键的存活期内我们仍然可以获取到它的Value。 "hello" redis 127.0.0.1:6379> ttl mykey #该ttl命令的返回值显示,该Key已经过期。 (integer) 0 redis 127.0.0.1:6379> get mykey #获取已过期的Key将返回nil。 (nil)
5. SETNX:
redis 127.0.0.1:6379> del mykey #删除该键,以便于下面的测试验证。 (integer) 1 redis 127.0.0.1:6379> setnx mykey "hello" #该键并不存在,因此该命令执行成功。 (integer) 1 redis 127.0.0.1:6379> setnx mykey "world" #该键已经存在,因此本次设置没有产生任何效果。 (integer) 0 redis 127.0.0.1:6379> get mykey #从结果可以看出,返回的值仍为第一次设置的值。 "hello"
6. SETRANGE/GETRANGE:
redis 127.0.0.1:6379> set mykey "hello world" #设定初始值。 OK redis 127.0.0.1:6379> setrange mykey 6 dd #从第六个字节开始替换2个字节(dd只有2个字节) (integer) 11 redis 127.0.0.1:6379> get mykey #查看替换后的值。 "hello ddrld" redis 127.0.0.1:6379> setrange mykey 20 dd #offset已经超过该Key原有值的长度了,该命令将会在末尾补0。 (integer) 22 redis 127.0.0.1:6379> get mykey #查看补0后替换的结果。 "hello ddrld\x00\x00\x00\x00\x00\x00\x00\x00\x00dd" redis 127.0.0.1:6379> del mykey #删除该Key。 (integer) 1 redis 127.0.0.1:6379> setrange mykey 2 dd #替换空值。 (integer) 4 redis 127.0.0.1:6379> get mykey #查看替换空值后的结果。 "\x00\x00dd" redis 127.0.0.1:6379> set mykey "0123456789" #设置新值。 OK redis 127.0.0.1:6379> getrange mykey 1 2 #截取该键的Value,从第一个字节开始,到第二个字节结束。 "12" redis 127.0.0.1:6379> getrange mykey 1 20 #20已经超过Value的总长度,因此将截取第一个字节后面的所有字节。 "123456789"
7. SETBIT/GETBIT:
redis 127.0.0.1:6379> del mykey (integer) 1 redis 127.0.0.1:6379> setbit mykey 7 1 #设置从0开始计算的第七位BIT值为1,返回原有BIT值0 (integer) 0 redis 127.0.0.1:6379> get mykey #获取设置的结果,二进制的0000 0001的十六进制值为0x01 "\x01" redis 127.0.0.1:6379> setbit mykey 6 1 #设置从0开始计算的第六位BIT值为1,返回原有BIT值0 (integer) 0 redis 127.0.0.1:6379> get mykey #获取设置的结果,二进制的0000 0011的十六进制值为0x03 "\x03" redis 127.0.0.1:6379> getbit mykey 6 #返回了指定Offset的BIT值。 (integer) 1 redis 127.0.0.1:6379> getbit mykey 10 #Offset已经超出了value的长度,因此返回0。 (integer) 0
8. MSET/MGET/MSETNX:
redis 127.0.0.1:6379> mset key1 "hello" key2 "world" #批量设置了key1和key2两个键。 OK redis 127.0.0.1:6379> mget key1 key2 #批量获取了key1和key2两个键的值。 1) "hello" 2) "world" #批量设置了key3和key4两个键,因为之前他们并不存在,所以该命令执行成功并返回1。 redis 127.0.0.1:6379> msetnx key3 "stephen" key4 "liu" (integer) 1 redis 127.0.0.1:6379> mget key3 key4 1) "stephen" 2) "liu" #批量设置了key3和key5两个键,但是key3已经存在,所以该命令执行失败并返回0。 redis 127.0.0.1:6379> msetnx key3 "hello" key5 "world" (integer) 0 #批量获取key3和key5,由于key5没有设置成功,所以返回nil。 redis 127.0.0.1:6379> mget key3 key5 1) "stephen" 2) (nil)
The above is the content of Redis tutorial (2): String data type. For more related content, please Follow the PHP Chinese website (www.php.cn)!

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.

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 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.

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.
