Redis basic data type (string)
Storage type
int (integer), float (single-precision floating-point number), string (string)
Common operation commands
set
-
command
SET key value [EX seconds] [PX milliseconds] [NX|XX]
-
Description
Set the key to the specified "string" value.
If the key has saved a value, then this operation will directly overwrite the original value and ignore the original type.
When the set command is executed successfully, the expiration time set before will be invalid -
Options
Starting from version 2.6.12, redis has added a series of options to the SET command:- EX seconds-Set the expiration time of the key, the unit is hour and second
- PX milliseconds-set the key expiration time, the unit is milliseconds
- NX-The value of the key will only be set if the key does not exist
- XX-The value of the key will only be set if the key exists
Note: Since the SET command plus options can completely replace the functions of SETNX, SETEX, PSETEX, in future versions, redis may be deprecated and eventually abandon these commands.
-
return value
If the
SET
command was executed normally and then return backOK
, or if addedNX
orXX
options, but do not set conditions. Then it will return nil. -
example
redis> SET mykey "Hello" OK redis> GET mykey "Hello" redis>
get
-
command
GET key
-
Description
Return
key
ofvalue
. If the key does not exist, return a special valuenil
. If itkey
isvalue
not a string, an error is returned, becauseGET
only strings of type string are processedvalues
. -
return value
The value corresponding to the key, or nil (when the key does not exist)
-
example
redis> GET nonexisting (nil) redis> SET mykey "Hello" OK redis> GET mykey "Hello" redis>
mset
-
command
MSET key value [key value ...]
-
Description
Correspond to the given keys to their corresponding values.
MSET
Will replace the existing value with the new value, just like a normal SET command. If you don't want to overwrite the existing values, please refer to the command MSETNX .MSET
It is atomic, so all given keys are set at once. It is impossible for the client to see that some of the keys have been updated and the others have not changed. -
return value
Always OK, because MSET will not fail.
-
example
redis> MSET key1 "Hello" key2 "World" OK redis> GET key1 "Hello" redis> GET key2 "World" redis>
mget
-
command
MGET key [key ...]
-
Description
Returns the value of all the specified keys. For each key that does not correspond to a string or does not exist, a special value is returned
nil
. Because of this, this operation never fails. -
return value
List of values corresponding to the specified key
-
example
redis> SET key1 "Hello" OK redis> SET key2 "World" OK redis> MGET key1 key2 nonexisting 1) "Hello" 2) "World" 3) (nil) redis>
getrange
-
command
GETRANGE key start end
-
Description
Warning : This command is changed to GETRANGE, called SUBSTR in Redis version less than 2.0. Return the substring of the string value corresponding to the key. This substring is determined by the displacement of start and end (both are in the string). You can use a negative displacement to represent the subscript starting from the end of the string. So -1 is the last character, -2 is the second to last character, and so on.
When this function handles out-of-range requests, it limits the result to string.
-
example
redis> SET mykey "This is a string" OK redis> GETRANGE mykey 0 3 "This" redis> GETRANGE mykey -3 -1 "ing" redis> GETRANGE mykey 0 -1 "This is a string" redis> GETRANGE mykey 10 100 "string" redis>
strlen
-
command
STRLEN key
-
Description
Returns the length of the string value of the key. If the key corresponds to a non-string type, an error is returned.
-
return value
The length of the string value corresponding to the key, or 0 (the key does not exist)
-
example
redis> SET mykey "Hello world" OK redis> STRLEN mykey (integer) 11 redis> STRLEN nonexisting (integer) 0 redis>
append
-
command
APPEND key value
-
Description
If
key
already exists, and the value is a string, then the command willvalue
be appended to the end of the original value (value) of. Ifkey
not present, it will first create an empty stringkey
, then perform additional operations, this situation APPEND will be similar to SET operation. -
return value
Returns the length of the string value (value) after append.
-
example
redis> EXISTS mykey (integer) 0 redis> APPEND mykey "Hello" (integer) 5 redis> APPEND mykey " World" (integer) 11 redis> GET mykey "Hello World" redis>
incr
-
command
INCR key
-
Description
key
Perform an atomic increment operation on the value stored in the specified value.If the specified key does not exist, its value will be set to before performing the incr operation
0
.If the value stored in the specified key is not a string type (fix:) or the stored string type cannot be represented as an integer,
Then the server will return an error when executing this command (eq:(error) ERR value is not an integer or out of range).
This operation is limited to 64-bit signed integer data.
Note : Since redis does not have a clear type to represent integer data, this operation is a string operation.
When performing this operation, the stored string corresponding to the key is parsed into decimal 64-bit signed integer data .
In fact, Redis internally uses an integer representation to store the corresponding integer value, so this type of string value is actually stored as an integer, so there is no string representation for storing integers. Additional consumption.
-
return value
The
key
corresponding value after the increment operation is performed . -
example
redis> SET mykey "10" OK redis> INCR mykey (integer) 11 redis> GET mykey "11" redis>
incrby
-
command
INCRBY key increment
-
Description
Add decrement to the number corresponding to the key. If the key does not exist, the key will be set to 0 before the operation. If the value type of the key is wrong or it is a string that cannot be represented as a number, an error is returned. This operation supports up to 64-bit signed positive numbers.
See the command INCR for additional information about the increase and decrease operations.
-
return value
The value after the increase.
-
example
redis> SET mykey "10" OK redis> INCRBY mykey 5 (integer) 15 redis>
incrbyfloat
-
command
INCRBYFLOAT key increment
-
Description
key
Increase the value of a floating-point number (stored in a string) by specifying a floating-point number . When the key does not exist, first set its value to 0 before operating. Any of the following will return an error:- The key contains an illegal value (not a string).
- The current key or the added value cannot be parsed as a double-precision floating point value (exceeding the precision range)
If the operation command is successful, the added value will replace the original value and store it on the corresponding key value, and return it in the type of string. The value stored in the string or the addition parameter can be arbitrarily selected with the exponential symbol, but the addition is calculated The result will be stored in the format of scientific notation. Regardless of the internal precision of each calculation, the output precision is fixed to 17 decimal places
-
return value
The current
key
value after increment is increased. -
example
redis> SET mykey 10.50 OK redis> INCRBYFLOAT mykey 0.1 "10.6" redis> SET mykey 5.0e3 OK redis> INCRBYFLOAT mykey 2.0e2 "5200" redis>
decr
-
command
DECR key
-
Description
Subtract 1 from the number corresponding to the key. If the key does not exist, the value corresponding to this key will be set to 0 before the operation. If the key has a value of the wrong type or a string that cannot be represented as a number, an error is returned. This operation supports up to 64-bit signed integer numbers.
See the command INCR for additional information about the increase and decrease operations.
-
return value
Value after reduction
-
example
redis> SET mykey "10" OK redis> DECR mykey (integer) 9 redis> SET mykey "234293482390480948029348230948" OK redis> DECR mykey ERR value is not an integer or out of range redis>
decrby
-
command
DECRBY key decrement
-
Description
Decrement the number corresponding to the key. If the key does not exist, the key will be set to 0 before the operation. If the value type of the key is wrong or it is a string that cannot be represented as a number, an error is returned. This operation supports up to 64-bit signed positive numbers.
See the command INCR for additional information about the increase and decrease operations.
-
return value
Return a number: the value after the reduction.
-
example
redis> SET mykey "10" OK redis> DECRBY mykey 5 (integer) 5 redis>
of the
-
command
DEL key [key ...]
-
Description
Delete the specified batch of keys. If some keys in the deletion do not exist, they will be ignored.
-
return value
The number of deleted keys
-
example
redis> SET key1 "Hello" OK redis> SET key2 "World" OK redis> DEL key1 key2 key3 (integer) 2 redis>