String type is the simplest type, a key corresponds to a value, and Stirng type is binary safe. Redis String can contain any data, such as jpg images or serialized objects.
Operation:
1,set
Set the value corresponding to key to the value of String type.
Example: Set a key-value pair of name=zhaojw, then get out the value of name, set name=zhaojw01 again, and then get to find that the value is overwritten:
127.0.0.1:6379> set name zhaojw
OK
127.0.0.1:6379> get name
"zhaojw"
127.0.0.1:6379> set name zhaojw01
OK
127.0.0.1:6379> get name
"zhaojw01"
2,setnx
Set the corresponding value to String type value. If the key already exists, return 0 without updating. nx means not exist.
Example: Set name=zhaojw02, found that return 0, get found that the value remains unchanged, when set age=26, set successfully.
127.0.0.1:6379> setnx name zhaojw02
(integer) 0
127.0.0.1:6379> get name
"zhaojw01"
127.0.0.1:6379> setnx age 26
(integer) 1
127.0.0.1:6379> get age
"26"
127.0.0.1:6379> setnx age 28
(integer) 0
127.0.0.1:6379> get age
"26"
3,setex
Set the value corresponding to the key to the value of String type, and specify the validity period corresponding to the key value.
Example: Add a key-value pair of haircolor=red and specify a validity period of 10 seconds. After 10 seconds, go to get and find that the return nil represents empty:
127.0.0.1:6379> setex haircolor 10 red
OK
127.0.0.1:6379> get haircolor
"red"
127.0.0.1:6379> get haircolor
(nil)
127.0.0.1:6379> setnx haircolor red
(integer) 1
127.0.0.1:6379> get haircolor
"red"
4,setrange
Set the substring of the value of the specified key. setrange key replaces the content from the number of positions (string subscripts, starting from 0), replacing only the content of the same length as the replacement content, and returns the length of the replaced string after successful replacement.
Example: Changing zhaojw's mailbox from 163 to 126:
127.0.0.1:6379> set mail zhaojw_420@163.com
OK
127.0.0.1:6379> get mail
"zhaojw_420@163.com"
127.0.0.1:6379> setrange mail 11 126
(integer) 18
127.0.0.1:6379> get mail
"zhaojw_420@126.com"
5,mset
Setting multiple key s at a time, returning ok successfully indicates that all values have been set successfully, and failing to return 0 means that no values have been set successfully.
127.0.0.1:6379> mset key1 zhaojw1 key2 zhaojw2 key3 zhaojw3
OK
127.0.0.1:6379> get key1
"zhaojw1"
127.0.0.1:6379> get key2
"zhaojw2"
127.0.0.1:6379> get key3
"zhaojw3"
6,msetnx
Setting multiple keys at a time, successfully returning ok means that all values are set successfully, and failure returning 0 means that none of the values are set successfully, but it will not cover the existing key. If one key is not set successfully, it will not be set successfully.
127.0.0.1:6379> msetnx key4 zhaojw4 key5 zhaojw5 key3 zhaojw
(integer) 0
127.0.0.1:6379> get key4
(nil)
127.0.0.1:6379> get key5
(nil)
127.0.0.1:6379> get key3
"zhaojw3"
127.0.0.1:6379> msetnx key4 zhaojw4 key5 zhaojw5 key6 zhaojw6
(integer) 1
127.0.0.1:6379> get key4
"zhaojw4"
127.0.0.1:6379> get key5
"zhaojw5"
127.0.0.1:6379> get key6
"zhaojw6"
7,get
Gets the String value corresponding to the key, and returns nil if the key does not exist.
getset
Set the value of key and return the old value of key:
127.0.0.1:6379> getset key6 zhaojw66
"zhaojw6"
127.0.0.1:6379> get key6
"zhaojw66"
8,getrange
A substring that gets the value value of the key. getrange key starts at the number of locations and ends at the number of locations
127.0.0.1:6379> getrange mail 0 5
"zhaojw"
127.0.0.1:6379> getrange mail 0 10
"zhaojw_420@"
9,mget
Get the value of multiple keys at a time, and return nil if the corresponding key does not exist.
127.0.0.1:6379> mget key1 key2 key3 key4 key5 key6
1) "zhaojw1"
2) "zhaojw2"
3) "zhaojw3"
4) "zhaojw4"
5) "zhaojw5"
6) "zhaojw66"
127.0.0.1:6379> mget key1 key2 key3 key4 key5 key6 key7
1) "zhaojw1"
2) "zhaojw2"
3) "zhaojw3"
4) "zhaojw4"
5) "zhaojw5"
6) "zhaojw66"
7) (nil)
10,incr
Do + + operation on the value of key and return the new value. The key that does not exist is 0 by default, and then the key=0 is equivalent to set, and then do + + operation.
127.0.0.1:6379> get age
"26"
127.0.0.1:6379> incr age
(integer) 27
127.0.0.1:6379> incr age
(integer) 28
127.0.0.1:6379> incr age
(integer) 29
127.0.0.1:6379> incr age
(integer) 30
127.0.0.1:6379> get age
"30"
127.0.0.1:6379> incr agee
(integer) 1
127.0.0.1:6379> incr agee
(integer) 2
127.0.0.1:6379> get agee
"2"
11,incrby
Similar to incr, when a specified value is added, the key is set when it does not exist and the original value is considered to be 0. Positive plus minus minus minus minus minus plus minus minus minus minus minus minus minus minus minus minus minus minus minus minus minus minus minus minus minus minus minus minus.
127.0.0.1:6379> get age
"30"
127.0.0.1:6379> incrby age 5
(integer) 35
127.0.0.1:6379> get age
"35"
127.0.0.1:6379> incrby ageee 10
(integer) 10
127.0.0.1:6379> get ageee
"10"
127.0.0.1:6379> incrby ageee -5
(integer) 5
127.0.0.1:6379> get ageee
"5"
12,decr
Do the operation on the value of the key. The same as incr. On the contrary, positive is minus, negative is plus.
127.0.0.1:6379> get ageee
"5"
127.0.0.1:6379> decr ageee
(integer) 4
127.0.0.1:6379> decr ageee
(integer) 3
127.0.0.1:6379> decr ageee
(integer) 2
127.0.0.1:6379> get ageee
"2"
13,decrby
Similar to incrby, subtract the specified value.
127.0.0.1:6379> get ageee
"2"
127.0.0.1:6379> decrby ageee -8
(integer) 10
127.0.0.1:6379> get ageee
"10"
127.0.0.1:6379> decrby ageee 5
(integer) 5
127.0.0.1:6379> get ageee
"5"
14,append
Appends value to the string of the specified key. Returns the length of the new string value, append key value
127.0.0.1:6379> get name
"zhaojw"
127.0.0.1:6379> append name &zhanr
(integer) 12
127.0.0.1:6379> get name
"zhaojw&zhanr"
15,strlen
Take the length of the value value of the specified key, strlen key
127.0.0.1:6379> strlen name
(integer) 12