Redis has five basic data structures: String, List, set, Hash, and zset
String (string)
A string represents a mutable array of bytes. Each element in the array is a single character. The maximum length of the string is 512 MB
At the beginning, we mentioned that Redis is an open source database of key-value, so when storing data, we need to set a key and store the data content as value
Create string [set key value]
> set test hello-world
OK
Copy the code
[get key]
> get test
"hello-world"
Copy the code
Having said that string represents a dynamic array of bytes, we now think of a set of strings as an array
Get string length * [strlen key]
> strlen test
(integer11)Copy the code
[getrange key start end]
> getrange test 0 4
"hello"
Copy the code
Override string [setrange key start value]
> setrange test 6 hi
> (integer11 / /test: "hello-hirld"
Copy the code
Append [append key value]
> append test -end
(integer15 / /)test: "hello-hirld-end"
Copy the code
Delete string [del key]
> del test
(integer1)Copy the code
Set string expiration time [expire key time]
The unit of time is second
> expire test 10
(integer1)Copy the code
Gets the remaining time of the string
> ttl test
(integer) 3
Copy the code
String operation [incrby key value, decrby key value]
Incrby test 1 can be written as INCR test, and decrby test 1 as DECr test
> set number 10
OK
> incrby number 10
(integer) 20
> decrby number 5
(integer15)Copy the code
List (list)
The storage structure of list is bidirectional linked list, so the random positioning ability is weak, and the insertion and deletion ability is strong
Supports negative subscripts. The subscripts of list can be 0, 1, 2, 3,…. Minus 1, minus 2, minus 2, minus 1, minus 2
Linked lists can append and remove elements from end to end, and lists can be used as queues and stacks
Rpush key value, Lpush key value, rPOP key, LPOP value
Rpush means add on the right, Lpush means add on the left, rPOP, LPOP
> rpush list 01 // list: 01
(integer) 1
> rpush list 02 // list: 01 02
(integer) 2
> lpush list 03 // list: 03 01 02
(integer) 3
> lpop list // list: 01 02
"3"
> rpop list // list: 01
"."
Copy the code
Get list length [llen key]
> llen list
(integer1)Copy the code
Access elements at specified positions [lrange key startIndex endIndex]
> lrange list 0 2
1) "01"
2) "12"
3) "11"> lrange list 0 -1"01"
2) "12"
3) "11"
Copy the code
Note: endIndex can also be represented with negative subscripts, as above
[lset key index value]
> lset list 0 13 // before: 01 12 11
OK // after: 13 12 11
Copy the code
Insert element [linsert key postion value newValue]
Note: Postion has two values before and after, which are inserted before and after a particular value
Since the insertion position is selected according to the specific value, the value position also changes as the list changes, so there are few scenarios to use
> < span style = "max-width: 100%; clear: both; min-height: 1em;integer) 4 // After: 13Copy the code
Delete element [LREM key count value]
Elements are also removed by value, and there may be more than one value of the same value, so there is a count parameter to indicate how many elements are removed
> < span style = "max-width: 100%; clear: both; min-height: 1em;integer) 1 // after: 13 12.5 12 11
Copy the code
Fixed length list [Ltrim Key startIndex endIndex]
Use to remove list elements other than startIndex and endIndex and keep elements in that range.
Often used to control the number of elements, such as the number of lottery winners
> ltrim list -3 -1 // before: 1 2 3 4 5
OK // after: 3 4 5
Copy the code
The llen, lrange, lset commands for lists are regular, such as llen, lrange, lset commands for lists prefixed with STR, llen, len, range commands for lists prefixed with L, and L is the type. For other commands, refer to the string command
Set
The elements of a set cannot be repeated, so they can be used for de-duplication, and the underlying set is a special hash
Add element [sadd key value]
You can add one or more elements at a time
> sadd fruits apple banana pear
(integer2)Copy the code
Get all elements [smembers key]
> smembers fruits
1) "apple"
2) "banana"
3) "pear"
Copy the code
Get set length [scard key]
> scard fruits
(integer) 3
Copy the code
Random fetch element [srandMember key count]
The command can randomly obtain a specified number of elements. If no number is specified after the command is executed, one element is randomly obtained by default
> srandmember fruits
"pear"
> srandmember fruits 2
1) "apple"
2) "pear"
Copy the code
Random delete [spop key count]
This command can delete a specified number of elements at random. If no number is specified after the command, one element is deleted at random by default
> spop fruits
"pear"
> spop fruits 2
1) "apple"
2) "banana"
Copy the code
[sismember key value]
Only one element can be queried by default
Sismember fruits apple (integer) 1 // There is no peach > sismember fruits peach (integer) 0
Copy the code
SortedSet
Sortedset (zset) assigns a weight score to each element value, and the internal elements will be automatically sorted according to the weight score. Elements can be obtained according to the score range, which can be used to filter the ranking
Add element [zadd key score value]
You can add one or more elements as shown below:
> zadd exam 60 Chinese (integer> Zadd Exam 90 Math 70 English (integer2)Copy the code
Get length [zcard key]
> zcard exam
(integer) 3
Copy the code
Delete element [zrem key value]
You can delete one or more elements
> zrem exam math (integerZrem exam Chinese English (integer2)Copy the code
Calculation [zincrby score key]
The elements here are similar to the elements in the string structure
"> < p style =" max-width: 100%; clear: both"91.5"
Copy the code
[zScore key value]
You can run the preceding command to query the weight of the specified value
> zscore exam Chinese
"60"
> zscore exam english
"70"
> zscore exam math
"91.5"
Copy the code
[zrevrank key value]
Zrevrank sorts according to the score from the smallest to the largest. The larger the returned value is, the larger the corresponding value weight is. Zrevrank sorts according to the weight from the largest to the smallest
> zrank exam math
(integer) 2
> zrank exam english
(integer) 1
> zrank exam Chinese
(integer) 0
> zrevrank exam math
(integer) 0
> zrevrank exam english
(integer) 1
> zrevrank exam Chinese
(integer2)Copy the code
[zrevrange key start end]
Zrevrange retrieves elements in a specified range from smallest to largest, and ZRevRange retrieves elements in a specified range from largest to smallest. Add the withscores parameter to show the weights
Zrange exam 0-1"Chinese"
2) "english"
3) "math"Exam 0-1 withscores 0)"Chinese"
2) "60"
3) "english"
4) "70"
5) "math"
6) "91.5"Zrevrange exam 0-1 withscores 1)"math"
2) "91.5"
3) "english"
4) "70"
5) "Chinese"
6) "60"
Copy the code
Get elements based on score range [ZrangebyScore key start end]
The -inf argument indicates negative infinity, +inf indicates positive infinity, and withscores indicates display weight.
> zrangebyscore exam 70 90
1) "english"
> zrangebyscore exam 70 90 withscores
1) "english"
2) "70"
> zrevrangebyscore exam 90 60 withscores
1) "english"
2) "70"
3) "Chinese"
4) "60"
> zrevrangebyscore exam +inf -inf withscores
1) "math"
2) "91.5"
3) "english"
4) "70"
5) "Chinese"
6) "60"
Copy the code
Note: when using the zrevrangebyscore command, the filter range should also be from large to small, as in the example above: zrevrangebyscore exam 90 60 withscores
Hash
Hash is similar to the Java language HashMap, Python dict, or JS object
Add elements [hset key field value | hmset field value] field value…
Hset can add a single element, and HMset adds multiple elements, where the field is similar to the key in the language structure above
Hset person name qiaoyu (integer) 1 // add multiple > hmset person sex man age 23 OKCopy the code
Get element [hset key field] [hmset key field1 field2]
Hset obtains the corresponding element according to the corresponding key, hmset obtains multiple corresponding elements according to the corresponding key, where the extra m can be understood as many meaning
> hget person name
"qiaoyu"
> hmget person name age sex
1) "qiaoyu"
2) "23"
3) "man"
Copy the code
Get all key/value pairs [hgetall key]
> hgetall person
1) "name"
2) "qiaoyu"
3) "age"
4) "23"
5) "sex"
6) "man"
Copy the code
Get all keys [hkeys]
> hkeys person
1) "name"
2) "age"
3) "sex"
Copy the code
Get all values [hvals]
> hvals person
1) "qiaoyu"
2) "23"
3) "man"
Copy the code
Delete element [hdel key]
Hdel can delete a single element or multiple elements
// Delete a single element > hdel person name (integer1 // Delete multiple elements > hdel person sex age (integer2)Copy the code
Determine whether an element exists [**hexists ** key]
Returns 1 for existence and 0 for non-existence
Hset Person name Tom age 15 (integer) 1
> hexists person name
(integer1)Copy the code
[Hincrby Key field increment]
Increment indicates the added value
> hincrby person age 2
(integer2)Copy the code