Redis tutorial (2): String data type

黄舟
Release: 2016-12-28 14:18:36
Original
1160 people have browsed it

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
Copy after login


2. INCR/DECR/INCRBY/DECRBY:
 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
Copy after login

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"
Copy after login

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)
Copy after login

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"
Copy after login

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"
Copy after login

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
Copy after login

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)
Copy after login

The above is the content of Redis tutorial (2): String data type. For more related content, please Follow the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!