Redis notes (i)-string data types

Source: Internet
Author: User
Tags truncated redis server



First, overview:

The string type is the most basic type of data storage in Redis, which is binary safe in Redis, which means that the type can accept data in any format, such as JPEG image data or JSON object description information. The value of the string type in Redis can accommodate up to 512M of data length.

Ii. List of related commands:


Command prototypes Complexity of Time return value Command description
APPEND Key value O (1) The length of the value after append If the key already exists, the append command appends the data for 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.
DECR key O (1) The value after descending Decrements the value of the specified key by 1. If the key does not exist, its initial value is 0, and after decr its value is-1. If the value of value cannot be converted to an integer value, such as Hello, the operation fails and returns the appropriate error message. Note: The value range for this operation is a 64-bit signed integer.
INCR Key O (1) The value after increment Increments the value of the specified key by 1. If the key does not exist, its initial value is 0, and after incr its value is 1. If the value of value cannot be converted to an integer value, such as Hello, the operation fails and returns the appropriate error message. Note: The value range for this operation is a 64-bit signed integer.
Decrby Key Decrement O (1) The value after reduction The value of the specified key is reduced by decrement. If the key does not exist, its initial value is 0, and after decrby its value is-decrement. If the value of value cannot be converted to an integer value, such as Hello, the operation fails and returns the appropriate error message. Note: The value range for this operation is a 64-bit signed integer.
Incrby Key Increment O (1) The value after the increment. Adds the value atomicity of the specified key to increment. If the key does not exist, its initial value is 0, and after incrby its value is increment. If the value of value cannot be converted to an integer value, such as Hello, the operation fails and returns the appropriate error message. Note: The value range for this operation is 64 bits to get the value of the specified key. If the Value associated with the key is not of type string, Redis returns an error message because the GET command can only be used to get string Value. Signed integral type.
GET Key O (1) The value associated with the key, if the key does not exist, returns nil.
SET Key value O (1) Always return "OK". Sets the key to hold the specified string value, overwriting its original value if the key already exists.
Getset Key value O (1) Returns the original value of the key, or nil if the key does not exist before. The atomic setting of the key is the specified value, and the original values of the key are returned. As with the Get command, the command can handle only string Value, or Redis will give you a related error message.
STRLEN Key O (1) Returns the value character length of the specified key, or 0 if the key does not exist. Returns the length of the character value for the specified key, and if value is not of type string, Redis will fail and give the associated error message.
Setex Key seconds value O (1) Atomicity completes two operations, one is to set the value of the key to the specified string, and set the key in the Redis server survival time (in seconds). This command is primarily used when Redis is used as a cache server.
Setnx Key value O (1) 1 indicates that the setting was successful, otherwise 0. If the specified key does not exist, the key is set to hold the specified string value, at which point the effect is equivalent to the SET command. Conversely, if the key already exists, the command will not take any action and return.
SETRANGE Key Offset value O (1) The modified string value length. Replaces the partial string value of the specified key. Starting with offset, the length of the replacement is the string length of the third parameter of the command value, where if the value of offset is greater than the string length of the original value of the key, Redis will be padded with value (Offset-strlen (value)) The number of 0x00, and then append the new value. If the key does not exist, the command assumes the length of its original value as 0, and then appends a new value after fill offset 0x00. Since the maximum length of string value is 512M, the maximum value for offset is 536870911. Finally, it is important to note that if the command is executed with an increase in the length of the original value of the specified key, it will cause Redis to reallocate enough memory to accommodate all of the replaced strings, resulting in some performance impairment.
GETRANGE Key Start end O (1) Sub-string If the truncated string is short, we can consider the time complexity of the command as O (1), or O (n), where n represents the length of the truncated substring. When the substring is truncated, the command will contain both start (0 for first character) and end character in a closed interval, and if the end value exceeds the character length of value, the command will simply intercept all character data after start.
Setbit Key Offset value O (1) The bit original value on the specified offset. Sets the value of bit on the specified offset, which can only be 1 or 0, after which the command returns the original bit value on that offset. If the specified key does not exist, the command creates a new value and sets the bit value in the parameter on the specified offset. If offset is greater than the character length of value, Redis will lengthen the value and set the bit value in the parameter on the specified offset, and the bit value added in the middle is 0. Finally, it should be stated that the value of offset must be greater than 0.
Getbit Key Offset O (1) The bit value on the specified offset. Returns the value of bit on the specified offset, 0 or 1. If offset exceeds the length of string value, the command returns 0, so always returns 0 for an empty string.
MGET key [Key ...] O (N) Returns a list of values for the specified keys. n indicates the number of keys to get. Returns all values of the specified keys, if one of them does not exist, or if the value is not of type string, the key's value returns nil.
MSET key value [key value ...] O (N) The command does not fail and always returns OK. n indicates the number of keys specified. The command atomically completes the set operation of all key/value in the parameter, and its specific behavior can be seen as executing the SET command multiple iterations.
Msetnx key value [key value ...] O (N) 1 means all keys are set successfully, and 0 means no key is modified. n indicates the number of keys specified. The command atomically completes the set operation of all key/value in the parameter, and its specific behavior can be seen as multiple iterations of the execution of the SETNX command. However, it is necessary to make it clear that if any of the keys in this batch are already present, the operation will all be rolled back, that is, all modifications will not take effect.


Examples of commands:

1. Set/get/append/strlen:

redis 127.0.0.1:6379> exists mykey #Determine if the key exists, return 1 if it exists, otherwise return 0.
    (integer) 0
    redis 127.0.0.1:6379> append mykey "hello" #The key does not exist, so the append command returns the length of the current Value.
    (integer) 5
    redis 127.0.0.1:6379> append mykey "world" #The key already exists, so the length of the Value after appending is returned.
    (integer) 11
    redis 127.0.0.1:6379> get mykey #Get the key through the get command to determine the result of append.
    "hello world"
    redis 127.0.0.1:6379> set mykey "this is a test" #Set the new value for the key with the set command and overwrite the original value.
    OK
    redis 127.0.0.1:6379> get mykey
    "this is a test"
    redis 127.0.0.1:6379> strlen mykey #Get the character length of the specified Key, which is equivalent to the strlen function in the C library.
    (integer) 14

    2. INCR / DECR / INCRBY / DECRBY:
    redis 127.0.0.1:6379> set mykey 20 #Set the value of Key to 20
    OK
    redis 127.0.0.1:6379> incr mykey #The value of this Key is incremented by 1
    (integer) 21
    redis 127.0.0.1:6379> decr mykey #the value of the key is decremented by 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 #Perform a decrement operation on the null value, its original value is set to 0, and the decremented value is -1
    (integer) -1
    redis 127.0.0.1:6379> del mykey
    (integer) 1
    redis 127.0.0.1:6379> incr mykey #Perform an increment operation on the null value, its original value is set to 0, and the incremented value is 1.
    (integer) 1
    redis 127.0.0.1:6379> set mykey hello #Set the value of this key to a normal string that cannot be converted to an integer.
    OK
    redis 127.0.0.1:6379> incr mykey #When performing an increment operation on this key again, Redis will report an error message.
    (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 #Increment the counter value by 1
    (integer) 1
    #While getting the original value of the counter and setting it to the new value, these two operations are done atomically at the same time.
    redis 127.0.0.1:6379> getset mycounter 0
    "1"
    redis 127.0.0.1:6379> get mycounter #View the result after setting.
    "0"

    4. SETEX:
    redis 127.0.0.1:6379> setex mykey 10 "hello" #Set the expiration time of the specified Key to 10 seconds.
    OK
    #Check the remaining survival time (seconds) of the specified Key through the ttl command. 0 means expired and -1 means never expired.
    redis 127.0.0.1:6379> ttl mykey
    (integer) 4
    redis 127.0.0.1:6379> get mykey #We can still get its Value during the lifetime of the key.
    "hello"
    redis 127.0.0.1:6379> ttl mykey #The return value of the ttl command shows that the key has expired.
    (integer) 0
    redis 127.0.0.1:6379> get mykey #Getting expired Key will return nil.
    (nil)

   5. SETNX:
    redis 127.0.0.1:6379> del mykey #Delete the key for the following test verification.
    (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" #This key already exists, so this setting has no effect.
    (integer) 0
    redis 127.0.0.1:6379> get mykey #It can be seen from the results that the returned value is still 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 #Replace 2 bytes from the sixth byte (dd has only 2 bytes)
    (integer) 11
    redis 127.0.0.1:6379> get mykey #View the replaced value.
    "hello ddrld"
    redis 127.0.0.1:6379> setrange mykey 20 dd #offset has exceeded the length of the original value of the Key, this command will add 0 at the end.
    (integer) 22
    redis 127.0.0.1:6379> get mykey #View the result of replacement after padding.
    "hello ddrld \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00dd"
    redis 127.0.0.1:6379> del mykey #Delete the Key.
    (integer) 1
    redis 127.0.0.1:6379> setrange mykey 2 dd #Replace empty values.
    (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 #Intercept the value of this key, starting with the first byte and ending with the second byte.
    "12"
    redis 127.0.0.1:6379> getrange mykey 1 20 # 20 has exceeded the total length of Value, so all bytes after the first byte will be 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 #Set the seventh BIT value calculated from 0 to 1, return the original BIT value 0
    (integer) 0
    redis 127.0.0.1:6379> get mykey #Get the result of the setting, the binary hexadecimal value of 0000 0001 is 0x01
    "\ x01"
    redis 127.0.0.1:6379> setbit mykey 6 1 #Set the sixth BIT value calculated from 0 to 1, return the original BIT value 0
    (integer) 0
    redis 127.0.0.1:6379> get mykey #Get the result of setting, the hexadecimal value of binary 0000 0011 is 0x03
    "\ x03"
    redis 127.0.0.1:6379> getbit mykey 6 #The BIT value of the specified offset is returned.
    (integer) 1
    redis 127.0.0.1:6379> getbit mykey 10 #Offset has exceeded the length of value, so it returns 0.
    (integer) 0

   8. MSET / MGET / MSETNX:
    redis 127.0.0.1:6379> mset key1 "hello" key2 "world" #The two keys of key1 and key2 are set in batches.
    OK
    redis 127.0.0.1:6379> mget key1 key2 #The values of key1 and key2 were obtained in batches.
    1) "hello"
    2) "world"
    #Built sets up the key3 and key4 keys, because they did not exist before, so the command is executed 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 sets key3 and key5, but key3 already exists, so this command fails to execute and returns 0.
    redis 127.0.0.1:6379> msetnx key3 "hello" key5 "world"
    (integer) 0
    #Get key3 and key5 in batches. Since key5 was not set successfully, it returns nil.
    redis 127.0.0.1:6379> mget key3 key5 

1) "stephen"
     2) (nil)

redis notes (a)-string data type

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.