Redis tutorial of ii: String data type

  • 2020-05-06 12:00:00
  • OfStack

Overview:

          string type is the most basic data storage type in Redis. It is binary safe in Redis, which means it can accept data in any format, such as JPEG image data or Json object description information. The maximum length of data that Value of string type can hold in Redis is 512M.

List of related commands:

命令原型
时间复杂度 命令描述 返回值
APPENDkeyvalue
O(1) 如果该Key已经存在,APPEND命令将参数Value的数据追加到已存在Value的末尾。如果该Key不存在,APPEND命令将会创建一个新的Key/Value。 追加后Value的长度。
DECRkey O(1)  将指定Key的Value原子性的递减1。如果该Key不存在,其初始值为0,在decr之后其值为-1。如果Value的值不能转换为整型值,如Hello,该操作将执行失败并返回相应的错误信息。注意:该操作的取值范围是64位有符号整型。 递减后的Value值。
INCRkey O(1)  将指定Key的Value原子性的递增1。如果该Key不存在,其初始值为0,在incr之后其值为1。如果Value的值不能转换为整型值,如Hello,该操作将执行失败并返回相应的错误信息。注意:该操作的取值范围是64位有符号整型。  递增后的Value值。 
DECRBYkey decrement   O(1)   将指定Key的Value原子性的减少decrement。如果该Key不存在,其初始值为0,在decrby之后其值为-decrement。如果Value的值不能转换为整型值,如Hello,该操作将执行失败并返回相应的错误信息。注意:该操作的取值范围是64位有符号整型。  减少后的Value值。
INCRBYkey increment   O(1)  将指定Key的Value原子性的增加increment。如果该Key不存在,其初始值为0,在incrby之后其值为increment。如果Value的值不能转换为整型值,如Hello,该操作将执行失败并返回相应的错误信息。注意:该操作的取值范围是64位有符号整型。  增加后的Value值。
GETkey  O(1)  获取指定Key的Value。如果与该Key关联的Value不是string类型,Redis将返回错误信息,因为GET命令只能用于获取string Value。  与该Key相关的Value,如果该Key不存在,返回nil。
SETkey value  O(1) 
设定该Key持有指定的字符串Value,如果该Key已经存在,则覆盖其原有值。 总是返回"OK"。
GETSETkey value O(1)  原子性的设置该Key为指定的Value,同时返回该Key的原有值。和GET命令一样,该命令也只能处理string Value,否则Redis将给出相关的错误信息。 返回该Key的原有值,如果该Key之前并不存在,则返回nil。
STRLENkey O(1) 返回指定Key的字符值长度,如果Value不是string类型,Redis将执行失败并给出相关的错误信息。 返回指定Key的Value字符长度,如果该Key不存在,返回0。
SETEXkey seconds value O(1) 原子性完成两个操作,一是设置该Key的值为指定字符串,同时设置该Key在Redis服务器中的存活时间(秒数)。该命令主要应用于Redis被当做Cache服务器使用时。  
SETNXkey value  O(1)  如果指定的Key不存在,则设定该Key持有指定字符串Value,此时其效果等价于SET命令。相反,如果该Key已经存在,该命令将不做任何操作并返回。 1表示设置成功,否则0。
SETRANGEkey offset value  O(1)  替换指定Key的部分字符串值。从offset开始,替换的长度为该命令第三个参数value的字符串长度,其中如果offset的值大于该Key的原有值Value的字符串长度,Redis将会在Value的后面补齐(offset - strlen(value))数量的0x00,之后再追加新值。如果该键不存在,该命令会将其原值的长度假设为0,并在其后添补offset个0x00后再追加新值。鉴于字符串Value的最大长度为512M,因此offset的最大值为536870911。最后需要注意的是,如果该命令在执行时致使指定Key的原有值长度增加,这将会导致Redis重新分配足够的内存以容纳替换后的全部字符串,因此就会带来一定的性能折损。  修改后的字符串Value长度。
GETRANGEkey start end O(1)  如果截取的字符串长度很短,我们可以该命令的时间复杂度视为O(1),否则就是O(N),这里N表示截取的子字符串长度。该命令在截取子字符串时,将以闭区间的方式同时包含start(0表示第一个字符)和end所在的字符,如果end值超过Value的字符长度,该命令将只是截取从start开始之后所有的字符数据。 子字符串 
SETBITkey offset value  O(1)  设置在指定Offset上BIT的值,该值只能为1或0,在设定后该命令返回该Offset上原有的BIT值。如果指定Key不存在,该命令将创建一个新值,并在指定的Offset上设定参数中的BIT值。如果Offset大于Value的字符长度,Redis将拉长Value值并在指定Offset上设置参数中的BIT值,中间添加的BIT值为0。最后需要说明的是Offset值必须大于0。  在指定Offset上的BIT原有值。
GETBITkey offset  O(1)  返回在指定Offset上BIT的值,0或1。如果Offset超过string value的长度,该命令将返回0,所以对于空字符串始终返回0。 在指定Offset上的BIT值。 
MGETkey [key ...]  O(N)  N表示获取Key的数量。返回所有指定Keys的Values,如果其中某个Key不存在,或者其值不为string类型,该Key的Value将返回nil。 返回一组指定Keys的Values的列表。
MSETkey value [key value ...]  O(N)  N表示指定Key的数量。该命令原子性的完成参数中所有key/value的设置操作,其具体行为可以看成是多次迭代执行SET命令。  该命令不会失败,始终返回OK。  
MSETNXkey value [key value ...]  O(N) N表示指定Key的数量。该命令原子性的完成参数中所有key/value的设置操作,其具体行为可以看成是多次迭代执行SETNX命令。然而这里需要明确说明的是,如果在这一批Keys中有任意一个Key已经存在了,那么该操作将全部回滚,即所有的修改都不会生效。 1表示所有Keys都设置成功,0则表示没有任何Key被修改。

Example of command:

    1. SET/GET/APPEND/STRLEN:
   


    /> redis-cli   # perform Redis Client tools.
    redis 127.0.0.1:6379> exists mykey                   # To determine whether the key exists, the presence returns 1 Otherwise return 0 .
    (integer) 0
    redis 127.0.0.1:6379> append mykey "hello"      # The key does not exist, therefore append Command returns current Value The length of the.
    (integer) 5
    redis 127.0.0.1:6379> append mykey " world"    # The key already exists, so the append is returned Value The length of the.
    (integer) 11
    redis 127.0.0.1:6379> get mykey                      # through get The command gets the key to determine append Results.
    "hello world"
    redis 127.0.0.1:6379> set mykey "this is a test" # through set The command sets a new value for the key and overrides the old value.
    OK
    redis 127.0.0.1:6379> get mykey
    "this is a test"
    redis 127.0.0.1:6379> strlen mykey                  # Access to specify Key Is equivalent to C In the library strlen Function.
    (integer) 14

      2. INCR/DECR/INCRBY/DECRBY:
 

    redis 127.0.0.1:6379> set mykey 20     # Set up the Key The value of 20
    OK
    redis 127.0.0.1:6379> incr mykey         # the Key Increasing the value of the 1
    (integer) 21
    redis 127.0.0.1:6379> decr mykey        # the Key The value of the decline 1
    (integer) 20
    redis 127.0.0.1:6379> del mykey          # Delete an existing key.
    (integer) 1
    redis 127.0.0.1:6379> decr mykey        # Decrement the null value, whose original value is set to 0 , the value after decreasing is -1
    (integer) -1
    redis 127.0.0.1:6379> del mykey  
    (integer) 1
    redis 127.0.0.1:6379> incr mykey        # Increments the null value, whose original value is set to 0 , the value after increment is 1
    (integer) 1
    redis 127.0.0.1:6379> set mykey hello # Will be the key Value Set to a normal string that cannot be converted to an integer.
    OK
    redis 127.0.0.1:6379> incr mykey        # When you increment the key again, Redis An error message will be reported.
    (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      # Increments the value of the counter atomically 1
    (integer) 1
    # Getting the original value of the counter and setting it to the new value atomically completes both operations.
    redis 127.0.0.1:6379> getset mycounter 0 
    "1"
    redis 127.0.0.1:6379> get mycounter       # View the results of the Settings.
    "0"
    
             
      4. SETEX:
 

    redis 127.0.0.1:6379> setex mykey 10 "hello"   # Set the specified Key The expiration time is 10 Seconds.
    OK   
    # through ttl The command looks at the specification Key The remaining survival time ( Number of seconds ) . 0 Which means it's expired, -1 It never expires.
    redis 127.0.0.1:6379> ttl mykey                      
    (integer) 4
    redis 127.0.0.1:6379> get mykey                      # We can still get the key during its lifetime Value .
    "hello"
    redis 127.0.0.1:6379> ttl mykey                        # the ttl The return value of the command shows the Key It's overdue.
    (integer) 0
    redis 127.0.0.1:6379> get mykey                      # Get expired Key Will return nil .
    (nil)

    5. SETNX:
   

    redis 127.0.0.1:6379> del mykey                      # Delete the key for the test validation below.
    (integer) 1
    redis 127.0.0.1:6379> setnx mykey "hello"        # The key does not exist, so the command executed successfully.
    (integer) 1
    redis 127.0.0.1:6379> setnx mykey "world"       # The key already exists, so this setting has no effect.
    (integer) 0
    redis 127.0.0.1:6379> get mykey                      # As you can see from the results, the value returned is still the same as the value set for the first time.
    "hello"

      6. SETRANGE/GETRANGE:
 

    redis 127.0.0.1:6379> set mykey "hello world"       # Set the initial value.
    OK
    redis 127.0.0.1:6379> setrange mykey 6 dd          # Start with the sixth byte 2 bytes (dd only 2 bytes )
    (integer) 11
    redis 127.0.0.1:6379> get mykey                         # View the value after the replacement.
    "hello ddrld"
    redis 127.0.0.1:6379> setrange mykey 20 dd        #offset Already exceeded Key The original value of the length, the command will be at the end of the complement 0 .
    (integer) 22
    redis 127.0.0.1:6379> get mykey                           # Check the filling 0 After the substitution.
    "hello ddrld\x00\x00\x00\x00\x00\x00\x00\x00\x00dd"
    redis 127.0.0.1:6379> del mykey                         # To remove the Key .
    (integer) 1
    redis 127.0.0.1:6379> setrange mykey 2 dd         # Replace the null value.
    (integer) 4
    redis 127.0.0.1:6379> get mykey                        # View the result after replacing the null value.
    "\x00\x00dd"  
    redis 127.0.0.1:6379> set mykey "0123456789"   # Set the new value.
    OK
    redis 127.0.0.1:6379> getrange mykey 1 2      # Intercepts the key Value , from the first byte to the end of the second byte.
    "12"
    redis 127.0.0.1:6379> getrange mykey 1 20   #20 Has more than Value , so all bytes after the first byte are truncated.
    "123456789"

      7. SETBIT/GETBIT:
 

    redis 127.0.0.1:6379> del mykey
    (integer) 1
    redis 127.0.0.1:6379> setbit mykey 7 1       # Settings from 0 The seventh place to start counting BIT A value of 1 , return to the original BIT value 0
    (integer) 0
    redis 127.0.0.1:6379> get mykey                # Gets the result of the Settings, binary 0000 0001 The hexadecimal value of 0x01
    "\x01"
    redis 127.0.0.1:6379> setbit mykey 6 1       # Settings from 0 The sixth place to start counting BIT A value of 1 , return to the original BIT value 0
    (integer) 0
    redis 127.0.0.1:6379> get mykey                # Gets the result of the Settings, binary 0000 0011 The hexadecimal value of 0x03
    "\x03"
    redis 127.0.0.1:6379> getbit mykey 6          # Returns the specified Offset the BIT Value.
    (integer) 1
    redis 127.0.0.1:6379> getbit mykey 10        #Offset beyond value Length of, so returns 0 .
    (integer) 0

    8. MSET/MGET/MSETNX:
   

    redis 127.0.0.1:6379> mset key1 "hello" key2 "world"   # Batch set key1 and key2 Two keys.
    OK
    redis 127.0.0.1:6379> mget key1 key2                        # Batch acquisition key1 and key2 The value of the two keys.
    1) "hello"
    2) "world"
    # Batch set key3 and key4 Two keys, because they didn't exist before, so the command executes successfully and returns 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"
    # Batch set key3 and key5 Two keys, but key3 Already exists, so the command fails to execute and returns 0 .
    redis 127.0.0.1:6379> msetnx key3 "hello" key5 "world"
    (integer) 0
    # Batch to obtain key3 and key5 Because of key5 Did not set successfully, so return nil .
    redis 127.0.0.1:6379> mget key3 key5                  
    1) "stephen"
    2) (nil)


Related articles: