Redis learning Manual (String data type)

Source: Internet
Author: User

I. Overview:

The string type is the most basic data storage type in Redis. It is binary safe in Redis, which means this type can accept data in any format, for example, JPEG image data or Json object description information. In Redis, the maximum data length of string-type values can be 512 MB.

Ii. Related command list:

Command prototype
Time Complexity Command description Return Value
APPENDKey value
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 creates a new Key/Value. The length of the appended Value.
DECRKey O (1) Decrease the Value atomicity of the specified Key by 1. If the Key does not exist, its initial value is 0 and its value is-1 after decr. If the Value cannot be converted to an integer Value, such as Hello, the operation fails and an error message is returned. Note: The value range of this operation is 64-bit signed integer. The Value after the decrease.
INCRKey O (1) Increments the Value atomicity of the specified Key by 1. If the Key does not exist, its initial value is 0 and its value is 1 after incr. If the Value cannot be converted to an integer Value, such as Hello, the operation fails and an error message is returned. Note: The value range of this operation is 64-bit signed integer. The incremental Value.
DECRBYKey decrement O (1) Reduces the decrement of the Value atomicity of the specified Key. If the Key does not exist, its initial value is 0 and its value is-decrement after decrby. If the Value cannot be converted to an integer Value, such as Hello, the operation fails and an error message is returned. Note: The value range of this operation is 64-bit signed integer. Value after reduction.
INCRBYKey increment O (1) Increases the Value atomicity of the specified Key. If the Key does not exist, its initial value is 0 and its value is increment after incrby. If the Value cannot be converted to an integer Value, such as Hello, the operation fails and an error message is returned. Note: The value range of this operation is 64-bit signed integer. The added Value.
GETKey O (1) Obtains the Value of a specified Key. If the Value associated with the Key is not of the string type, Redis returns an error message because the GET command can only be used to obtain the string Value. Value related to the Key. If the Key does not exist, nil is returned.
SETKey value O (1)
Set the Key to hold the specified string Value. If the Key already exists, it overwrites the original Value. Always Returns "OK ".
GETSETKey value O (1) Set the Key as the specified Value and return the original Value of the Key. Like the GET command, this command can only process string values, otherwise Redis will provide related error information. Returns the original value of the Key. If the Key does not exist before, nil is returned.
STRLENKey O (1) Returns the length of the character Value of the specified Key. If the Value is not of the string type, Redis will fail to execute and provide related error information. Returns the length of the Value of the specified Key. If the Key does not exist, 0 is returned.
SETEXKey seconds value O (1) Atomicity completes two operations. One is to set the value of the Key to a specified string and the survival time (in seconds) of the Key on 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, the Key is SET to hold the specified string Value. The effect is equivalent to the SET command. On the contrary, if the Key already exists, the command will not perform any operation and return it. 1 indicates that the setting is successful; otherwise, 0 is returned.
SETRANGEKey offset value O (1) Replaces some string values of the specified Key. Starting from offset, the replacement length is the string length of the third parameter value of the command. If the offset Value is greater than the string length of the original value of the Key, redis will add 0x00 (offset-strlen (Value) after the value, and then chase the new Value. If the key does not exist, the command assumes that the length of its original value is 0, and adds offset 0x00 to the end, and then traces the new value. Since the maximum length of a string Value is 512 M, the maximum offset Value is 536870911. Note that if the command increases the length of the original value of the specified Key during execution, Redis will re-allocate enough memory to accommodate all the strings after replacement, therefore, it will cause a certain performance loss. The length of the modified string Value.
GETRANGEKey start end O (1) If the length of the intercepted string is very short, the time complexity of this command is considered as O (1); otherwise, it is O (N). Here N indicates the length of the intercepted substring. This command contains the start(0 indicates the first character)And end. If the end Value exceeds the length of the Value, this command only intercepts all the character data after start. Substring
SETBITKey offset value O (1) Set the BIT value on the specified Offset. The value can only be 1 or 0. After the setting, the command returns the original BIT value on the Offset. If the specified Key does not exist, this 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. The BIT Value added in the middle is 0. The Offset value must be greater than 0. The original BIT value on the specified Offset.
GETBITKey offset O (1) Returns the BIT value on the specified Offset, 0 or 1. If the Offset value exceeds the length of the string value, the command returns 0, so 0 is always returned for null strings. The BIT value on the specified Offset.
MGETKey [key...] O (N) N indicates the number of keys to be obtained. Returns the Values of all specified Keys. If a Key does not exist or its Value is not of the string type, the Value of the Key returns nil. Returns the list of Values of a group of specified Keys.
MSETKey value [key value...] O (N) N indicates the number of specified keys. This command is atomic to complete the setting of all the key/value in the parameter. The specific behavior can be seen as multiple iterations of the SET command. This command will not fail and always returns OK.
MSETNXKey value [key value...] O (N) N indicates the number of specified keys. This command is atomic to complete the setting of all the key/value in the parameter. The specific behavior can be seen as multiple iterations of the SETNX command. However, it must be clearly stated that,If any Key already exists in this batch of Keys, this operation will be rolled back, that is, all modifications will not take effect. 1 indicates that all Keys are set successfully, and 0 indicates that no Key is modified.

Iii. Command example:

1. SET/GET/APPEND/STRLEN:
/> Redis-cli # Run the Redis client tool.
Redis 127.0.0.1: 6379>Exists mykey # checks whether the key exists. If yes, 1 is returned. Otherwise, 0 is returned.
(Integer) 0
Redis 127.0.0.1: 6379>Append mykey "hello"# This 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" # This key already exists, so the length of the appended Value is returned.
(Integer) 11
Redis 127.0.0.1: 6379>Get mykey # obtain the key using the get command to determine the append result.
"Hello world"
Redis 127.0.0.1: 6379>Set mykey "this is a test" # Use the set command to set a new value for the key 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 # gets 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 Key value to 20
OK
Redis 127.0.0.1: 6379>Incr mykey# The value of this Key increases by 1.
(Integer) 21
Redis 127.0.0.1: 6379>Decr mykey # the value of this Key decreases 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 # the original value of the decimal value is set to 0, and the decimal value is-1.
(Integer)-1
Redis 127.0.0.1: 6379>Del mykey
(Integer) 1
Redis 127.0.0.1: 6379>Incr mykey # increment the null value. The original value is set to 0 and the incremental value is 1.
(Integer) 1
Redis 127.0.0.1: 6379>Set mykey hello # Set the Value of the key to a common string that cannot be converted to an integer.
OK
Redis 127.0.0.1: 6379>Incr mykey # When the incremental operation is performed again on the key, Redis reports 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 # increase the atomicity of the counter value by 1
(Integer) 1
# Obtain the original counter value and set it to a new value. The two operations are atomic at the same time.
Redis 127.0.0.1: 6379>Getset mycounter 0
"1"
Redis 127.0.0.1: 6379>Get mycounter# View the set result.
"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
# Use the ttl command to check the remaining survival time (in seconds) of the specified Key. 0 indicates that the Key has expired, and-1 indicates that the Key will never expire.
Redis 127.0.0.1: 6379>Ttl mykey
(Integer) 4
Redis 127.0.0.1: 6379>Get mykey# We can still obtain the Value of the key during its storage period.
"Hello"
Redis 127.0.0.1: 6379>Ttl mykey# The returned value of the ttl command indicates that the Key has expired.
(Integer) 0
Redis 127.0.0.1: 6379>Get mykey# Returning an expired Key will return nil.
(Nil)

5. SETNX:
Redis 127.0.0.1: 6379>Del mykey # Delete this 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 is successfully executed.
(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# From the result, we can see 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 only has 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 completing 0.
"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 null 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 a new value.
OK
Redis 127.0.0.1: 6379>Getrange mykey 1 2 # truncate the Value of the key, starting from 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. Therefore, 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# Set the seventh BIT value calculated from 0 to 1, and return the original BIT value 0
(Integer) 0
Redis 127.0.0.1: 6379>Get mykey# Obtain the set result. The hexadecimal value of binary 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, and return the original BIT value 0
(Integer) 0
Redis 127.0.0.1: 6379>Get mykey# Obtain the set result. The hexadecimal value of binary 0000 0011 is 0x03.
"\ X03"
Redis 127.0.0.1: 6379>Getbit mykey 6 # returns the BIT value of the specified Offset.
(Integer) 1
Redis 127.0.0.1: 6379>Getbit mykey 10 # Offset has exceeded the value length, so 0 is returned.
(Integer) 0

8. MSET/MGET/MSETNX:
Redis 127.0.0.1: 6379>Mset key1 "hello" key2 "world"# Set keys key1 and key2 in batches.
OK
Redis 127.0.0.1: 6379>Mget key1 key2# Obtain the values of key1 and key2 in batches.
1) "hello"
2) "world"
# Set the keys key3 and key4 in batches. Because they do not exist before, the command is successfully executed and 1 is returned.
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"
# If two keys key3 and key5 are set in batches, but key3 already exists, the command fails to be executed and 0 is returned.
Redis 127.0.0.1: 6379>Msetnx key3 "hello" key5 "world"
(Integer) 0
# Obtain key3 and key5 in batches. nil is returned because key5 is not set successfully.
Redis 127.0.0.1: 6379>Mget key3 key5
1) "stephen"
2) (nil)

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.