As we all know, Redis has five storage structures: String, List, Set, Zset and Hash
catalogue
1.1 single character writing and query
1.2 batch string writing and query
2.2 stack (right in right out)
1. String
Redis's string is a dynamic string that can be modified. Its internal structure is similar to ArrayList in Java. It uses pre allocation of redundant space to reduce frequent memory allocation. As shown in the figure, the internal space capacity actually allocated for the current string is generally higher than the actual string length len. When the string length is less than 1M, the expansion is to double the existing space. If it exceeds 1M, only 1M more space will be expanded at a time. It should be noted that the maximum length of the string is 512M.
1.1 single character writing and query
Write string set name zhangsan
Query string get name
1.2 batch string writing and query
Batch write string mset name1 zhangsan name2 lisi
Batch query string mget name1 name2
Reading and writing multiple strings in batch can save network time and overhead
1.3 deletion
If deleted, only a single deletion is supported
1.4 set expiration time
Set the expiration time for the key in seconds:
① expire name 5 (the name expires in 5 seconds. You can access the name during this period, but you can't get the value of name after expiration)
② setex name 5 zhangsan (equivalent to expire+set, add the expiration time when setting the value)
1.5 setnx
setnx command (set if not exists): set the value when it does not exist
(set the value zhangsan to name, and then use setnx to set the value lisi to name. Because the key name exists, the setnx setting does not work, and the obtained name value is zhangsan.)
(delete the name value first, and then setnx name. At this time, because the key name does not exist, setnx is set successfully)
setnx is also often used for distributed locks
Last time I used setnx to prevent duplicate submission (if setnx (key) = = 1, this record has been saved and will not be processed)
1.6 numerical calculation
Self increment: incr age
Plus fixed value: incrby age # 5
Add floating point value: incrbyfloat by 2.5
Self subtraction: decr age
Minus fixed value: decr age 5
* the maximum value of value is the maximum value of Long. If it is exceeded, an error will be reported
1.7 string splicing
append name san
1.8 bitmap
redis also plays a very important role in implementing bitmap
The data is stored in bitmap. If it can be found in bitmap, this data may exist; If bitmap does not exist, then this data must not exist. Judge in advance before accessing the database to reduce the access pressure of the database.
2. list
Redis's list is equivalent to the LinkedList in the Java language. Note that it is a linked list rather than an array. This means that the insertion and deletion operations of the list are very fast, and the time complexity is O(1), but the index positioning is very slow, and the time complexity is O(n).
A list can contain up to 232 - 1 elements (4294967295, with more than 4 billion elements per list).
list mainly has two operations: push (save data) and pop (get data), and then there are two directions, left and right
list is often used as message queue
2.1 queue (right in left out)
Save data on the right: rpush # names # zhangsan lisi wangwu
Data on the left: lpop names
This realizes the function of queue
2.2 stack (right in right out)
Save data on the right: rpush # names # zhangsan lisi wangwu
Data on the right: rpop names
2.3 viewing elements
In the past, the elements were taken out directly. Now it is to view the elements without taking them out
The first element of the query list: lindex list1 0
The second element of the query list: lindex list1 1
The last element of the query list: lindex list1 - 1
Query list 1-3 elements: lrange list1 0 2
Query all elements of the list: lrange list1 0 -1
Query list length: len LIST1
2.4 get elements
Get the first data from list1. If you can't get the data, wait for 5 seconds: blpop list1 5
Get the last piece of data from list2. If you can't get the data, wait for 5 seconds: brpop list1 5
Insert the last data in list1 into the first data in list2 and return the data. If there is no data in list1, wait 5 seconds and return:
brpoplpush list1 list2 5
2.5 insert elements
Insert ddd before bbb in the list: linsert list before bbb ddd
Of course, you can also insert 333: linsert list after bbb 333 after bbb
Insert 222: lset list 2 222 at the second value
2.6 removing elements
Remove two a's from the list: lrem list 2 a
Only the elements in the range of 2-5 in the original list are retained: ltrim list 2-5
3.Hash
Hash is the most used type of redis except string
Each hash in Redis can store 2 ^ 32 - 1 key value pairs (more than 4 billion)
3.1 setting value
Set a value for users: hset users name zhangsan age 18
When gender has no value, assign a value to gender: hsetnx users gender M
3.2 get value
Get user's name: hget user name
Get user's name and age: hmget user name and age
Get all attributes in user: hkeys user
Get the number of all attributes in user: hlen user
Get all attribute values in user: hvals user
Get all key value pairs in user: hgetall user
Check whether there is attribute xxx in user: hexists user xxx
test:0>hset user name zhangsan age 18 gender M "0" test:0>hget user name "zhangsan" test:0>hmget user name age 1) "zhangsan" 2) "18" test:0>hkeys user 1) "name" 2) "age" 3) "gender" test:0>hlen user "3" test:0>hvals user 1) "zhangsan" 2) "18" 3) "M" test:0>hgetall user 1) "name" 2) "zhangsan" 3) "age" 4) "18" 5) "gender" 6) "M" test:0>
3.3 value calculation
Add 1: hincrby user age 1 to age
Add 2.5 to age: hincrbyfloat user age 2.5
test:0>hincrby user age 1 "19" test:0>hincrbyfloat user age 2.5 "21.5" test:0>
3.4 deletion
Delete name and age in user: HDEL user name and age
test:0>hdel user name age "2" test:0>hkeys user 1) "gender" test:0>
4. set
set is equivalent to HashSet in java, which is unordered
4.1 basic operation
test:0>sadd set1 a b c a d f b #Add elements to set1. Because the values in set are not repeated, only 5 elements are added "5" test:0>scard set1 # Get the number of elements in set1, which can be used to store the likes, and then get the likes "5" test:0>sismember set1 a # Determine whether set1 contains element a "1" test:0>sismember set1 g # Determine whether set1 contains element g "0" test:0>spop set1 # Pop up an element randomly and return "f" test:0>srandmember set1 2 # Take out 2 elements at any time in set1, which is often used in lucky draw 1) "d" 2) "a" test:0>srem set1 b c # Remove the elements b and c in set1. You can remove only one or more "2"
4.2 collection operation
test:0>sadd set1 a b c d e "5" test:0>sadd set2 d e f g "4" test:0>sdiff set1 set2 # Returns the difference between set1 and set2 1) "c" 2) "b" 3) "a" test:0>sdiff set2 set1 # Returns the difference between set2 and set1 1) "f" 2) "g" test:0>sunion set1 set2 # Returns all elements in set1 and set2 1) "f" 2) "g" 3) "d" 4) "e" 5) "c" 6) "b" 7) "a" test:0>sunionstore set3 set1 set2 # Return all the elements in set1 and set2 and store them in set3 "7" test:0>smembers set3 # Query all elements in set3 1) "f" 2) "g" 3) "d" 4) "e" 5) "c" 6) "b" 7) "a" test:0>sinter set1 set2 # Returns the union of elements in set1 and set2 1) "e" 2) "d" test:0>sinterstore set4 set1 set2 # Returns the union of elements in set1 and set2 and stores them in set4 "2" test:0>smembers set4 # Query all elements in set4 1) "e" 2) "d" test:0>
5.zset(sorted set)
Redis ordered collections, like collections, are collections of string elements, and duplicate members are not allowed.
The difference is that each element is associated with a score of type double. redis sorts the members of the collection from small to large through scores.
Members of an ordered set are unique, but scores can be repeated.
The set is implemented through hash table, so the complexity of adding, deleting and searching is O(1). The maximum number of members in the collection is
2 ^ 32 - 1 (4294967295, each set can store more than 4 billion members).
test:0>zadd chengji 60 zhangsan 60 lisi 70 wangwu 80 zhaoliu 90 mazi # Add a value to chengji "5" test:0>zrangebylex chengji - + # Returns the members of an ordered set according to the dictionary order, - represents the minimum and + represents the maximum 1) "lisi" 2) "zhangsan" 3) "wangwu" 4) "zhaoliu" 5) "mazi" test:0>zrangebyscore chengji 0 100 # Returns an ordered set with a score between 0 and 100 1) "lisi" 2) "zhangsan" 3) "wangwu" 4) "zhaoliu" 5) "mazi" test:0>zrangebyscore chengji 0 100 withscores # Returns an ordered set and score with a score between 0 and 100 1) "lisi" 2) "60" 3) "zhangsan" 4) "60" 5) "wangwu" 6) "70" 7) "zhaoliu" 8) "80" 9) "mazi" 10) "90" test:0>zrank chengji zhangsan # Return the ranking and index of zhangsan "1" test:0>zcard chengji # Gets the number of members in chengji "5" test:0>zcount chengji 70 90 # Calculate the number of members with a score between 70-90 (inclusive) "3" test:0>zrangebyscore chengji 70 90 withscores # Returns members and scores with a score between 70 and 90 1) "wangwu" 2) "70" 3) "zhaoliu" 4) "80" 5) "mazi" 6) "90" test:0>zincrby chengji 10 zhangsan # score+10 for zhangsan "70" test:0>zscore chengji zhangsan # Return zhangsan's score "70" # Calculate the number of members between the dictionary order lisi and zhangsan, [means included, (means not included) test:0>zlexcount chengji [lisi [zhangsan "3" test:0>zrangebylex chengji [lisi [zhangsan # View the members between lisi and zhangsan 1) "lisi" 2) "wangwu" 3) "zhangsan" # Calculate the number of members between the dictionary order lisi and zhangsan, (meaning that zhangsan is not included) test:0>zlexcount chengji [lisi (zhangsan "2" test:0>zrangebylex chengji [lisi (zhangsan # View the members between lisi and zhangsan 1) "lisi" 2) "wangwu" test:0>zrank chengji zhangsan # View zhangsan's ranking "2" test:0>zrange chengji 0 5 withscores # View members and score s in articles 0-5 1) "lisi" 2) "60" 3) "wangwu" 4) "70" 5) "zhangsan" 6) "70" 7) "zhaoliu" 8) "80" 9) "mazi" 10) "90" test:0>zrem chengji zhangsan lisi # Remove zhangsan and lisi "2" test:0>zrange chengji 0 5 withscores # Check the members and score s in articles 0-5. There are no zhangsan and lisi 1) "wangwu" 2) "70" 3) "zhaoliu" 4) "80" 5) "mazi" 6) "90" test:0>zremrangebyrank chengji 0 1 # Remove members ranking 0-1 (i.e. wangwu and zhaoliu) "2" test:0>zrange chengji 0 5 withscores # Check the members and score s in articles 0-5. There are no wangwu and zhaoliu 1) "mazi" 2) "90" test:0>zadd chengji 20 zhangsan 30 lisi 50 wangwu # Add zhangsan, lisi, wangwu "3" test:0>zrange chengji 0 5 withscores # View members and score s in articles 0-5 1) "zhangsan" 2) "20" 3) "lisi" 4) "30" 5) "wangwu" 6) "50" 7) "mazi" 8) "90" test:0>zremrangebyscore chengji 0 10 # Remove members with score between 0-10 "0" test:0>zremrangebyscore chengji 0 20 # Remove members with a score between 0-20 "1" test:0>zrange chengji 0 5 withscores # View members and score s in articles 0-5 1) "lisi" 2) "30" 3) "wangwu" 4) "50" 5) "mazi" 6) "90" test:0>zrevrange chengji 0 5 withscores # View members and score s in reverse order from 0 to 5 1) "mazi" 2) "90" 3) "wangwu" 4) "50" 5) "lisi" 6) "30" test:0>zrevrangebyscore chengji 100 50 withscores # View the members with a score between 100-50 and their scores in reverse order 1) "mazi" 2) "90" 3) "wangwu" 4) "50" test:0>zrevrangebyscore chengji 100 0 withscores # View the members and scores whose score is between 100-0 in reverse order 1) "mazi" 2) "90" 3) "wangwu" 4) "50" 5) "lisi" 6) "30" test:0>zrevrank chengji mazi # View the ranking of mazi in reverse order "0" test:0>zrevrank chengji lisi # View in reverse order "2" test:0>
To sum up:
lex is used to view dictionary order
rev is used to reverse the order
6. Others
redis has 16 databases 0-15 by default. When it is opened by default, the first database will be operated. Select the fifth database: select 5
Main references: Redis string | rookie tutorial