Redis data type-five data types: string, list, set, zset, hash1 ). string is the simplest type. A key corresponds to a value. String is binary secure. Redis string can contain any data, such as JPG images or serialized objects (Object serialization function serialize in PHP)
Redis data type-five data types: string, list, set, zset, hash 1 ). string is the simplest type. A key corresponds to a value. String is binary secure. Redis string can contain any data, such as JPG images or serialized objects (Object serialization function serialize in PHP)
Redis data type -- string
Five data types: string, list, set, zset, and hash
1). string type
String is the simplest type. A key corresponds to a value. The string type is binary secure. Redis string can contain any data, such as JPG images or serialized objects (Object serialization function serialize in PHP)
The internal implementation is essentially a byte array, and the size of the string is limited to MB
Www.2cto.com
[Plain]
Struct sdshdr {
Long len; // The length of the buf Array
Long free; // number of available bytes in the buf Array
Char buf []; // stores the actual string content
}
Operation Method:
A. set Method:
Format: set key value
Set the value corresponding to the key to the string type value, OK = successful, 0 = failed. If the key already exists, overwrite the original value.
[Plain]
> Set pwd 123456 // Add a k-v with pwd = 123456
OK
B. get method:
Format: get key
Returns the string value corresponding to the key. If the key does not exist, nil is returned.
[Plain]
> Get pwd // obtain the value of pwd.
"123456"
> Get name
(Nil)
C. setnx method:
Format: setnx key value
It is the same as set. The difference is: Check whether the key already exists before setting. If the key already exists, 0 is returned (nx = notexist)
Www.2cto.com
[Plain]
> Setnx user zhangsan
(Integer) 1
> Setnx user lisi
(Integer) 0
> Get user
"Zhangsan"
> Set user chuangrain
OK
> Get user
"Chuangrain"
D. setex method:
Format: setex key seconds value
Set the value corresponding to the key to a value of the string type, and specify the validity period of seconds for this key value
[Plain]
> Setex tea 10 food // you can set tea = food to specify the effective time of 10 seconds.
OK
> Get tea
"Food"
> Get tea // after 10 s
(Nil)
E. setrange method:
Format: setrange key offset value
Www.2cto.com
Use the value parameter to replace the string value corresponding to the specified key. Starting from offset, the nonexistent key is treated as a blank string. If the offset is greater than the string length corresponding to the key, then, the blank space between the original string and the offset is filled with zero bits (zerobytes, "\ x00"), and the length of the string modified by setrange is returned.
[Plain]
> Set hw "hello word"
OK
> Setrange hw 6 "Redis"
(Integer) 11
> Get hw
"HelloRedis"
> Setrange hello 2 "chuang" // hello does not exist
(Integer) 12
> Get hello
"\ X00 \ x00chuangrain"
> Setrange hello 15 rd
(Integer) 17
> Get hello
"\ X00 \ x00chuangrain \ x00 \ x00 \ x00rd"
F. mset Method
Format: mset key1 value1 key2 value2...
If multiple key values are set at a time, OK is returned. If all values are set successfully, 0 is returned for failure, that is, no setting is returned. This operation is atomic.
G. msetnx method:
Format: msetnx key1 value1 key2 value2...
Similar to mset, the difference is to check whether the set key already exists. If yes, 0 is returned.
H. getset method:
Format: getset key value
Set the value of the string type corresponding to the key and return the old value of the key. If the key does not exist, set key = value first and then return nil.
Www.2cto.com
[Plain]
> Get site
"Taobao"
> Getset site baidu
"Taobao"
> Get site
"Baidu"
I. getrange method:
Format: getrange key start end
Obtain the substring of the value corresponding to the key, starting from start to end. If end (positive) is less than start (positive), or the key does not exist, the return value is a Null String "", if end exceeds the length of value, return the string between start and end of value. start and end can be negative, and the subscript on the left of the string starts from 0, the subscript on the right starts from-1.
0
1
2
3
4
B
A
I
D
U
-5
-4
-3
-2
-1
Take "baidu" as an example, that is, each character may be accessed through positive or negative
[Plain]
> Get site
"Baidu"
> Getrange site 1 2
"Ai"
> Getrange site 3-1
"Du"
> Getrange www 3 4 // www does not exist
""
J. mget Method
Format: mget key1 key2...
Obtain the value corresponding to multiple keys at a time. If the corresponding key does not exist, the corresponding nil is returned.
Www.2cto.com
[Plain]
> Mset user1 taobao user2 baidu
OK
> Mget user1 user2 user3 // user3 does not exist
1) "taobao"
2) "baidu"
3) (nil)
K. incr method:
Format: incr key
Increments the value atomicity of the specified key by 1, and returns the incremental value. If the key does not exist, set its initial value to 0 and increase by 1. If the value is not an integer, a failure message is returned.
Www.2cto.com
[Plain]
> Mset key1 44 key2 1.1 key3 taobao
OK
> Incr key1
(Integer) 45
> Get key1
"45"
> Incr key2
(Error) ERR value is not an integer or out of range
> Incr key3
(Error) ERR value is not an integer or out of range
> Incr key4 // key4 does not exist
(Integer) 1
L. incrby method:
Format: incrby key decrement
Similar to incr, the incremental decrement of the value atomicity corresponding to the specified key is returned. If the key does not exist, set the initial value to 0 and then increment the decrement.
M. decr method:
Format: decr key
In contrast to incr, the value corresponding to the key is decreased by 1. If the key does not exist, set the key to the initial value of 0 and then decrease by 1.
[Plain]
> Decrdeepin // The deepin does not exist.
(Integer)-1
N. decrby method:
Www.2cto.com
Format: decrby key decrement
Opposite to incrby
O. strlen method:
Format: strlen key
Obtains the string length of the value corresponding to the key. If the key does not exist, nil is returned.
P. append method:
Format: append key value
If the key already exists, append the data of the value to the end of the existing value. If the key does not exist, a new key/value is created. Returns the length of the new value.
Www.2cto.com
[Plain]
> Append site taobao
(Integer) 11
> Get site
"Baidutaobao"
> Append test taobao // test does not exist
(Integer) 6
> Get test
"Taobao"
Q. setbit method:
Format: setbit key offset value
Set the bit value (value) on the specified offset corresponding to the key. The value can only be 1 or 0, and the original bit value on the offset is returned. If the key does not exist, a new value is created and the bit value is set on the specified offset. If offset is greater than the string length of value, redis will lengthen the value and set the bit value in the parameter on the specified offset. The bit value added between them is 0. Note: The offset value must be greater than 0.
Www.2cto.com
[Plain]
> Setbit userkey 7 1 // The userkey does not exist. from 0 to 2nd, the bit value is 1, and the original bit value 0 is returned.
(Integer) 0
> Get userkey
"\ X01" // convert 0000 0001 to hexadecimal
R. getbit method:
Format: getbit key offset
Get the bit value of the offset corresponding to the key. If the offset is greater than the length of the value, or the key does not exist, or a non-binary string, 0 is returned.
Www.2cto.com
[Plain]
> Getbit userkey 7
(Integer) 1
> Getbit userkey // user1key does not exist
(Integer) 0