This is the 10th day of my participation in the August More Text Challenge. For details, see:August is more challenging”
String type is the most basic data type in Redis. It can store any form of string, including binary data, serialized data, jsonized objects and even a picture.
The general idea of string data manipulation is to manipulate value by key, where key is the data id, and value is the business data that we’re interested in.
💍 1 set
-
Syntax: set key value
-
Run the following command to set the string value to a key. If the key already exists, the preceding value will overwrite the preceding value.
-
Return value: OK indicates success
2, get 💎
-
Syntax: get key
-
Run the following command to obtain the string value set in key
-
Return value: key exists, return value corresponding to key;
Key doesn’t exist, return nil
⚽ 3, append
-
Syntax: append key value
-
Function: Append value to the end of the original value of the key, if the key exists
If the key does not exist, the key is set to value
-
Return value: total length (number of characters) after the append string
⚾ 4, strlen
-
Syntax: strlen key
-
Function: returns the length of the string value stored by key
-
Return value: The length of the string value if the key exists;
Key does not exist, return 0
🥎 5, incr
-
Syntax: incr key
-
Run the following command to increment the numeric value stored in the key by 1. If the key does not exist, the value of the key is initialized to 0 before the incr operation is performed.
-
Return value: returns the key value incremented by 1
🏀 6, decr
-
Syntax: decr key
-
If the key does not exist, then the value of the key is initialized to 0 before the decr operation is performed.
-
Return value: Returns the key value minus 1
7, incrby 🎯
-
Syntax: incrby key offset
-
Run the following command to add an increment value to the stored value of the key. If the key does not exist, the value of the key is initialized to 0 and then the INCRBY command is executed.
-
Return value: Returns the value of the key after the increment.
8, decrby 🏸
-
Syntax: decrby key offset
-
Run the DECRBY command to subtract the decrement value from the stored key. If the key does not exist, the value of the key is initialized to 0 before the DECRBY command is executed.
-
Return value: Returns the decrement value of the key.
Nine, getrange 🏓
-
Syntax: getrange key startIndex endIndex
-
Run the following command to obtain the substring of the key from startIndex to endIndex, including startIndex and endIndex. A negative number indicates that the value starts from the end of the string, and -1 indicates the last character.
10, setrange 🥊
-
Syntax: setrange key offsetIndex value
-
Function: Overwrite the stored value of the key with value starting with offset.
-
Return value: the length of the modified string.
🥋 11, setex
-
Syntax: setex key seconds value
-
Run the following command to set the value of the key and set the lifetime of the key to seconds. If the key already exists, the old value is overwritten.
-
Return value: Set successfully, return OK.
12, setnx ⛳
-
Syntax: setnx key value
-
Function: setnx is short for set if Not exists. If the key does not exist, the value is set. If the key does exist, the value is not set.
-
Return value: 1 on success, 0 on failure
13, mset 🥇
-
Syntax: mset key value [key value…
-
Run the following command to set one or more key-value pairs at the same time
-
Return value: Set successfully, return OK.
14 and mget 🥈
-
Syntax: mget key [key…]
-
Function: Gets all (one or more) values of a given key
-
Return value: a list of all keys, or nil if the key does not exist.
15, msetnx 🏆
-
Syntax: msetnx key value[key value…
-
Run the following command to set one or more key-value pairs at the same time. If one key exists, the setting fails.
-
Return value: set successfully, return 1
Failed to set. 0 is returned