Redis data type is string command sorting and example, redisstring
# SET key value [EX seconds] [PX milliseconds] [NX | XX]
SET
Commands and options can completely replace the functions of SETNX, SETEX, and PSETEX. In future versions, redis may not recommend these commands and eventually discard them.
So I will introduce the different modes of the set in sequence, and will not introduce how to GET the String value from GET in the instance.
# Conventional
127.0.0.1:6379> set Current 2018-03-04OK127.0.0.1:6379> get Current"2018-03-04"
# Set a key with a life time
# Set CurrentHaveTimeOut to S.
127.0.0.1:6379> set CurrentHaveTimeOut test Ex 120OK
# Check the time, and s will be deleted
127.0.0.1:6379> ttl CurrentHaveTimeOut(integer) 108
# Get it again in 2 minutes
127.0.0.1:6379> get CurrentHaveTimeOut(nil)
# This parameter can be set only when the key does not exist.
127.0.0.1:6379> set CurrentHaveNx test NXOK
# Repeated settings
127.0.0.1:6379> set CurrentHaveNx test NX(nil)
# The opposite of NX is XX, which can be modified only when the key exists.
# Setting fails if the value does not exist
127.0.0.1:6379> set CurrentHaveXx test XX(nil)
# Set Value
127.0.0.1:6379> set CurrentHaveXx test1OK
# Modification and resetting in XX mode successful
127.0.0.1:6379> set CurrentHaveXx test XXOK127.0.0.1:6379> get CurrentHaveXx"test"
# Tips: the NX mode can be used to create redis locks. NXSET is used to determine whether the lock exists.
# APPEND the String value to the APPEND key value
Ifkey
And the value is a string.value
Append to the end of the original value. Ifkey
If it does not exist, it will first create an empty stringkey
And then perform the append operation.
127.0.0.1:6379> EXISTS key(integer) 0127.0.0.1:6379> APPEND key gavin(integer) 5127.0.0.1:6379> get key"gavin"127.0.0.1:6379> APPEND key jun(integer) 8127.0.0.1:6379> get key"gavinjun"
# Replacing SETRANGE key offset value with a specified position string
This command overwrites a part of the string corresponding to the key and overwrites the length of the value starting from the specified offset. If the offset value is longer than the string value corresponding to the current key, the value 0 is added after the string value to offset the value. The Nonexistent keys are considered as null strings, so this command can ensure that the key has a string that is large enough to set the value at the offset.
# Normal condition: the offset cannot exceed the length
127.0.0.1:6379> set key1 "hello world!"OK127.0.0.1:6379> get key1"hello world!"127.0.0.1:6379> SETRANGE key1 6 redis(integer) 12127.0.0.1:6379> get key1"hello redis!"
# Case 2: The String does not exist, and it exists, but the offset value is greater than the String length.
# If it does not exist, the system will perform the 0-filling operation.
127.0.0.1: 6379> SETRANGE key2 3 test (integer) 7127.0.0.1: 6379> get key2 "\ x00 \ x00 \ x00test" # offset is greater than the string length, similar to the above, all are prefill 0127.0.0.1: 6379> set key3 tOK127.0.0.1: 6379> get key3 "t" 127.0.0.1: 6379> SETRANGE key3 3 go (integer) 5127.0.0.1: 6379> get key3 "t \ x00 \ x00go"
# Obtain the string length of the key STRLEN key
Returns the length of the string type value of the key. If the key is not of the string type, an error is returned.
127.0.0.1: 6379> STRLEN key3 (integer) 5 # example of a non-string type 127.0.0.1: 6379> LPUSH list 1 (integer) 1127.0.0.1: 6379> LRANGE list 0-11) "1" 127.0.0.1: 6379> STRLEN list (error) WRONGTYPE Operation against a key holding the wrong kind of value # Return 0127.0.0.1: 6379> STRLEN key4 (integer) 0 if the key does not exist
# Introduce the INCR key together with auto-incrementing auto-subtraction and GETSET first, and then reset and put them together
Forkey
To perform the atomic plus 1 operation.
If the specified key does not exist, the value of the key is set0
.
If the value stored in the specified key is not a string type (fix :) or the stored string type cannot be expressed as an integer,
When the command is executed, the server returns an error (eq :( error) ERR value is not an integer or out of range ).
This operation is only applicable to 64-bit signed integer data.
# If 1key does not exist, it will automatically increase from 0 to 127.0.0.1: 6379> INCR key5 (integer) 1127.0.0.1: 6379> get key5 "1" # 2key cannot be converted to 127.0.0.1: 6379> set key7 te316127.0.0.1: 6379> INCR key7 (error) ERR value is not an integer or out of range # When 3key is a floating point number, the result is consistent with 127.0.0.1: 6379> set key6 3.1OK127.0.0.1: 6379> INCR key6 (error) ERR value is not an integer or out of range # case 4 normal 127.0.0.1: 6379> set key4 1OK127. 0.0.1: 6379> get key4 "1" 127.0.0.1: 6379> INCR key4 (integer) 2127.0.0.1: 6379> get key4 "2"
DECR key
Subtract 1 from the number corresponding to the key. If the key does not exist, the value corresponding to the key is set to 0 before the operation. If the key has an error type value or a string that cannot be expressed as a number, an error is returned. This operation supports up to 64-bit signed integer numbers.
# Do not describe it. The scenario is the same as that of auto-incrementing INCR.
GETSET key value
Automatically maps the key to the value and returns the value corresponding to the original key. If the key exists but the corresponding value is not a string, an error is returned.
127.0.0.1:6379> INCR key8(integer) 1127.0.0.1:6379> GETSET key8 2"1"127.0.0.1:6379> get key8"2"
# Batch setting and retrieval are omitted, similar to set. For details, refer to the redis manual.