Introduction to the
The simplest data structure, equivalent to a String in JavaCopy the code
Basic operation
-
SET key value Sets the value of the string key to value. The command returns OK indicating success. If the string key already exists, override the old value with the new value
127.0.0.1:6379> set MSG "hello world" OK 127.0.0.1:6379> get MSG "hello world" 127.0.0.1:6379> set MSG "hi" OK 127.0.0.1:6379 > get MSG "hi"Copy the code
SET the key value [NX | XX] NX: command only in key key does not exist, to SET up operation; If the key already exists, then SET… The NX command does not take any action (the old value will not be overwritten) XX: the command sets the value only when the key already exists. If the key does not exist, SET… XX command does no action (must overwrite old values)
Given the NX option and XX option, the SET command returns OK on success and nil on failure
Sample:
- NX:
127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> set msg hi NX OK 127.0.0.1:6379> set msg hi NX (nil) Copy the code
- XX
127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> set msg hi XX (nil) 127.0.0.1:6379> set msg hi OK 127.0.0.1:6379> set msg hi2 XX OK 127.0.0.1:6379> get msg "hi2" Copy the code
- NX:
-
GET Key Returns the value stored by the string key
127.0.0.1:6379 > get MSG hi2 ""Copy the code
-
SETNX key Value Set the key value to value only when the key does not exist. The effect is the same as set key value NX
-
MSET key value [ key value … ] Setting values for one or more string keys at a time has the same effect as executing multiple sets simultaneously
127.0.0.1:6379> MSET msg1 value1 msg2 value2 msg3 value3 OK Copy the code
-
MSETNX key value [ key value … ] MSETNX sets values for all given keys only if none are present, as if multiple SETNX were executed simultaneously. If at least one of the given keys exists, MSETNX does not perform any setup operations
-
MGET key [ key … ] Returns the value of one or more string keys at a time, the same effect as executing multiple GET commands simultaneously
127.0.0.1:6379> mget msg1 msg2 msg3 1) "value1" 2) "value2" 3) "value3" Copy the code
-
GETSET key new-value Sets the value of the string key to new-value and returns the old value stored by the string key before setting the new value.
127.0.0.1:6379> get msg "hi" 127.0.0.1:6379> getset msg hello "hi" 127.0.0.1:6379> get msg "hello" Copy the code
-
APPEND key Value Appends content to the end of the string. Pushes the value value to the end of the stored content of the string key. Returns the length of the appended value
127.0.0.1:6379> get MSG "hello" 127.0.0.1:6379> Append MSG "world" (integer) 11 127.0.0.1:6379> get MSG "hello world"Copy the code
-
STRLEN key Returns the length of the value stored in the string key
127.0.0.1:6379> strlen msg (integer) 11 Copy the code
The index
- The index of a string starts with 0 and increases from the beginning of the string to the end of the string. The index of the first character is 0, and the index of the last character is N-1, where N is the length of the string
- In addition to positive indexes, strings also have negative indexes: negative indexes start with -1 and decrease from the end of the string to the beginning of the string. The index of the last character of the string is -n, where N is the length of the string
- SETRANGE key index valueOverwrite the string value stored for the given key with value, starting with index index. Only positive indexes are accepted. The command returns the length of the string value after the overwrite is returned
127.0.0.1:6379> get msg "hello world" 127.0.0.1:6379> setrange msg 6 XX (integer) 11 127.0.0.1:6379> get msg "hello XXrld" Copy the code
- GETRANGE key start endReturns the contents of the string value stored by key between the start and end indexes. Indexes can be positive or negative
127.0.0.1:6379> getrange MSG 5 11 "XXrld" 127.0.0.1:6379> getrange msg-5-1 "XXrld"Copy the code
Digital operation
As long as the value stored in the string key can be interpreted as a 64-bit integer or ieEE-754 64-bit floating-point number, the user can execute commands for numeric values on the string key
value | Can execute digital commands | why |
---|---|---|
123456 | can | Values can be interpreted as integers |
3.14 | can | Values can be interpreted as floating point numbers |
+ 123 | can | Values can be interpreted as integers |
123456789123456789123456789 | Can not be | Value too large to be stored as a 64-bit integer |
2.0 e7 | Can not be | Cannot explain floating point numbers represented by scientific notation |
123AAA | Can not be | Values contain characters |
AAA | Can not be | The value is string |
-
INCRBY key increment Increment Returns the current key value after the operation is executed
127.0.0.1:6379> get msg "10" 127.0.0.1:6379> incrby msg 5 (integer) 15 127.0.0.1:6379> get msg "15" Copy the code
-
DECRBY Key Decrement Subtracts the values stored in the key and returns the current key values after the operation
127.0.0.1:6379> get msg "15" 127.0.0.1:6379> decrby msg 2 (integer) 13 127.0.0.1:6379> get msg "13" Copy the code
If the key does not exist when INCRBY and DECRBY are executed, the command initializes the key value to 0, and then increments or decays the key
-
INCR key If 1 is added to the value of the current storage key, it is the same as executing INCRBY key 1
127.0.0.1:6379> get MSG "13" 127.0.0.1:6379> incr MSG (integer) 14 127.0.0.1:6379> get MSG "14" 127.0.0.1:6379> get MSG "14"Copy the code
-
DECR key If the value of the current stored key is reduced by 1, it is the same as executing DECRBY key 1
127.0.0.1:6379> get msg "14" 127.0.0.1:6379> decr msg (integer) 13 127.0.0.1:6379> get msg "13" Copy the code
-
INCRBYFLOAT key increment Increment is the stored value of the string key plus the increment of the floating point number
127.0.0.1:6379> set num 11 OK 127.0.0.1:6379> incrbyfloat num 0.14 "11.14" 127.0.0.1:6379> get num "11.14" 127.0.0.1:6379> incrbyfloat num -1.82 "9.32" 127.0.0.1:6379> get num "9.32" Copy the code
Binary data manipulation
Commands such as SET, GET, SETINX, and APPEND can be used to SET binary data
-
The indexAs with text, the string key stores binary bits with an index that starts at zero, but instead of increasing the index from left to right, the string key stores binary bits with an index that decreases from left to right
-
SETBIT key index value sets the binary value of the given index to value. The SETBIT key index value command returns the old value stored in the SETBIT
-
GETBIT key Value Returns the value of the binary bits on the given index
-
BITCOUNT key [start] [end] Calculates and returns the number of binary digits set to 1 in the string key-value store
-
BITOP operation destkey key [ key … ] One or more string keys that hold binary bits perform bitwise operations and save the results to destkey. Operation One of the four operations: AND, OR, NOR, OR XOR
The command | The effect |
---|---|
BITOP AND destkey key [ key … ] | Obtain the logical union of one or more keys and save the result to the destkey |
BITOP OR destkey key [ key … ] | Add one or more keys to obtain logical or and save the result to destkey |
BITOP XOR destkey key [ key … ] | Obtain logical XOR from one or more keys and save the result to the Destkey |
BITOP NOT destkey key [ key … ] | One or more keys are obtained and the result is saved to the destkey |
Note for Storing Chinese
-
STRLEN, SETRANGE, and GETRANGE are set for English characters and work only when the character is a single byte. These three commands do not apply to multi-byte Chinese characters
-
When using Chinese in redis- CLI, you must turn on the — RAW option to display Chinese normally
127.0.0.1:6379> set MSG "hello" OK 127.0.0.1:6379> get MSG "\xe4\ XBD \xa0\xe5\xa5\ XBD "127.0.0.1:6379> strlen MSG (integer) 6Copy the code
Using the — raw
[root@localhost utils]# redis-cli --raw 127.0.0.1:6379> auth 123456 OK 127.0.0.1:6379> get MSG hello 127.0.0.1:6379> strlen msg 6Copy the code