Let’s warm up with redis-key

The command Meaning and return value
set Associate the string value value with key. If key already holds another value, SET overwrites the old value, regardless of the type. When the SET command sets a TTL for a key, the TTL for the key is cleared.
move Moves the key of the current database to the given database DB. If the current database (the source database) and the given database (the target database) have a given key with the same name, or the key does not exist in the current database,So MOVE has no effect. Therefore, MOVE can also be used as a locking primitive. Returns 1 on success or 0 on failure.
The exists Checks whether the given key exists. Return value 1 if key exists, 0 otherwise.
get Returns the string value associated with the key.
expire A lot of stuff
ttl Returns the TTL (time to live) of a given key, in seconds. Return Value -2 if key does not exist. If the key exists but the remaining lifetime is not set, -1 is returned. Otherwise, the remaining lifetime of the key is returned in seconds.
type Returns the type of the value stored by key. Return values: None (key does not exist), string (string), list (list), set (set), zset (ordered set), hash (hash table), stream (stream)
127.0.0.1:6379 >setThe name kittyguy OK 127.0.0.1:6379 >setAge 100 OK 127.0.0.1:6379> keys * 1)"age"
2) "name"127.0.0.1:6379 > the EXISTS of the ageCheck whether the key exists
(integer) 1
127.0.0.1:6379> EXISTS agee
(integer) 0
127.0.0.1:6379> move name 3   Move the key to the corresponding database
(integer) 1
127.0.0.1:6379> select 3
OK
127.0.0.1:6379[3]> get name
"kittyguy"127.0.0.1:6379 [3] > expire name 12Set the key's longevity in seconds
(integer) 1
127.0.0.1:6379[3]> ttl name     # Check the remaining longevity of the corresponding key
(integer) 7
127.0.0.1:6379[3]> ttl name
(integer) 5
127.0.0.1:6379[3]> ttl name
(integer) 4
127.0.0.1:6379[3]> ttl name
(integer) 3
127.0.0.1:6379[3]> ttl name
(integer) 1
127.0.0.1:6379[3]> ttl name
(integer) 0
127.0.0.1:6379[3]> ttl name # The end of life
(integer) -2
127.0.0.1:6379[3]> get name  # no more key127.0.0.1:6379[3]> SELECT 0 OK 127.0.0.1:6379>type age  Select value from key
string
Copy the code

Operations on strings

Concatenation operation

The command Meaning and return value
append If the key already exists and its value is a string, the APPEND command appends the value to the end of the key’s existing value. If the key doesn’t exist, APPEND simply sets the key’s value to value, as if it were SET key value. Return value: the length of the key value after appending value.
strlen Obtain the value length of the corresponding key
127.0.0.1:6379 >setName Java OK 127.0.0.1:6379> Append name" is good"   # additional
(integer) 12
127.0.0.1:6379> get name
"java is good"127.0.0.1:6379 > strlen nameGet the value length of the corresponding key
(integer) 12
127.0.0.1:6379> keys *
1) "name"127.0.0.1:6379 > append the age of 123456If the corresponding key does not exist, the append operation creates it
(integer) 6
127.0.0.1:6379> keys *
1) "age"
2) "name"
Copy the code

Increment and decrement operation

The command Meaning and return value
incr Add one to the numeric value stored for the key. If the key does not exist, its value is initialized to 0 before INCR is executed. If the value stored by the key cannot be interpreted as a number, the INCR command returns an error. The value of this operation is limited to 64-bit (bit) signed numeric representations.
decr Subtract one from the numeric value stored for the key. If the key does not exist, the value of the key is initialized to 0 before DECR is performed. If the value stored by the key cannot be interpreted as a number, the DECR command returns an error. The value of this operation is limited to 64-bit (bit) signed numeric representations.
incrby Add increment to the numeric value stored for the key. If the key does not exist, the value of the key is initialized to 0 before the INCRBY command is executed.
decrby Subtract the integer values stored for the key key from decrement. If the key does not exist, the value of the key is initialized to 0 before the DECRBY command is executed. If the value stored by the key cannot be interpreted as a number, the DECRBY command returns an error.
127.0.0.1:6379 >setCount 0 OK 127.0.0.1:6379> incr count# 1
(integer) 1
127.0.0.1:6379> get count
"1"127.0.0.1:6379 > decr countA decrease of 1 #
(integer) 0
127.0.0.1:6379> get count
"0"127.0.0.1:6379 > decr count (integer1 127.0.0.1:6379> decr count (integer) 2 127.0.0.1:6379> incrby count 4# increase step size by 4
(integer) 2                     
127.0.0.1:6379> get count   
"2"                          # - 2 + 4 = 2127.0.0.1:6379 > decrby count of 10Reduce the step size to 10
(integer) -8           
127.0.0.1:6379> get count
"Eight"                         # 2-10 = 8
Copy the code

Intercept the replacement operation

The command Meaning and return value
getrange Returns the specified portion of the string value stored by key. The range of the string is determined by the start and end offsets (both included). A negative offset means counting from the end of the string,-1 indicates the last character, -2 indicates the penultimate character, and so on. 
setrange Please read it carefully
setex Set the value of the key to value and the lifetime of the key to seconds. If the key already exists, the SETEX command overwrites the existing value.
setnx Set the key value to value only if the key does not exist. If the key already exists, the SETNX command does nothing. SETNX is”SET the if Not eXists“(if none exists, SET).
127.0.0.1:6379 >set str "0123456789"OK 127.0.0.1:6379> getrange STR 25Interception but value does not change
"2345"127.0.0.1:6379> getrange STR 2-1I'm going to go straight to the end
"23456789"127.0.0.1:6379 >setStr2 aaaaaaaa OK 127.0.0.1:6379> setrange str2 3 KKK# replace key follows the initial subscript to be replaced
(integer) 8
127.0.0.1:6379> get str2            
"aaakkkaa"                          # Replace as many as you have127.0.0.1:6379 > setex str3 12"redis"    # set the key value to value, and set the key survival time
OK                                       Set this parameter to seconds.
                                         If the key already exists, the SETEX command overwrites the existing valueTTL str3 (127.0.0.1:6379 >integer) 5
127.0.0.1:6379> ttl str3
(integer) 4
127.0.0.1:6379> ttl str3
(integer) 3
127.0.0.1:6379> ttl str3
(integer) 1
127.0.0.1:6379> ttl str3
(integer) 0
127.0.0.1:6379> ttl str3
(integer) -2
127.0.0.1:6379> get str3
(nil)
127.0.0.1:6379> keys *
1) "str2"
2) "str"127.0.0.1:6379 > setnx str3"kitty"Is "SETifShort for "Not exists" (if it does Not exist, SET). If present, do not operate (integer) 1
127.0.0.1:6379> setnx str3 "7878787"
(integer) 0
127.0.0.1:6379> get str3
"kitty"
Copy the code

Multivalued operation

The command Meaning and return value
mset Set values for multiple keys simultaneously. If a given key already exists, MSET will override the old value with the new value. If this is not what you want, consider using itMSETNXCommand, which will only set if all given keys are not present. MSET is an atomic operation. All given keys are set at the same time. There is no case where some keys are set but others are not.
mget Returns the value of one or more string keys given. If a given string key does not exist, the value of that key will be nil.
msetnx Sets a value for all given keys if and only if none of the given keys exist. The MSETNX command refuses to set all keys even if only one given key already exists. MSETNX is an atomic operation where all given keys are either set or not set at all.There can be no third state. Return value: If all the given keys are set successfully, the command returns 1. If the setting fails because a given key already exists, the command returns 0.
127.0.0.1:6379 > mset STR"jk" str1 "mk" str2 "kitty" str3 "guy"  # add multiple k-vs at once
OK
127.0.0.1:6379> keys *
1) "str1"
2) "str"
3) "str2"
4) "str3"
127.0.0.1:6379> mget str1 str2 str3   # query multiple K's at once
1) "mk"
2) "kitty"
3) "guy"127.0.0.1:6379 > msetnx STR"hH" str1 "love" str4 "false"  # Atomic operations, either all fail or all succeed
(integer) 0                                           # str4 does not exist in this example, so fail, 0 fail, 1 succeed127.0.0.1:6379> mget str1 str2 str3"mk"
2) "kitty"
3) "guy"127.0.0.1:6379 >Copy the code

Operations on objects (JSON)

127.0.0.1:6379 > mset user: 1: id"454545445446pp" user:1:name "jonh" user:1:address "China"
OK   Set the user:1 object
127.0.0.1:6379> mget user:1:id user:1:name user:1:address
1) "454545445446pp"
2) "jonh"
3) "China"127.0.0.1:6379 > getset user: 1: address"USA"   # select old value, set new value, null also can be set
"China"127.0.0.1:6379 > get user: 1: address"USA"127.0.0.1:6379 >Copy the code