brief introduction
summary
As a key/value data storage system, Redis provides a variety of data types to facilitate data management. Then, the data generated in our project is stored based on the specified type, such as user login information, shopping cart information, commodity details, etc.
Common data types
The basic data structures in IDS include strings, hashes, lists, collections, and ordered collections. The specific type used in the work should be combined with the specific scene.
String type operation practice
String type is the simplest data type in redis. Its stored value can be string, and its maximum string length can be up to 512M. Based on this type, you can realize the word count of a blog, continuously append the log to the specified key, realize a distributed self increasing iid, and realize the like operation of a blog
incr/incrby
When the stored string is an integer, redis provides a practical command INCR to increment the current key value and return the incremented value.
Syntax: INCR key
127.0.0.1:6379> set num 1 (integer) 1 127.0.0.1:6379> incr num (integer) 2 127.0.0.1:6379> keys * 1) "num" 127.0.0.1:6379> incr num 127.0.0.1:6379>
Note: if num does not exist, it will be created automatically. If num exists, it will be automatically + 1.
Specify growth factor
Syntax: INCRBY key increment
127.0.0.1:6379> incrby num 2 (integer) 5 127.0.0.1:6379> incrby num 2 (integer) 7 127.0.0.1:6379> incrby num 2 (integer) 9 127.0.0.1:6379>
decr/decrby
Decrements the specified integer
DECR key is decremented according to the default step size (1 by default)
Decrby key increment decrements according to the specified step size
127.0.0.1:6379> incr num (integer) 10 127.0.0.1:6379> decr num (integer) 9 127.0.0.1:6379> decrby num 3
append
Append a value to the tail. If the key does not exist, the key is created, and its value is the written value, that is, it is equivalent to SET key value. The return value is the total length of the appended string.
Syntax: APPEND key value
127.0.0.1:6379> keys * 1) "num" 2) "test1" 3) "test" 127.0.0.1:6379> get test "123" 127.0.0.1:6379> append test "abc" (integer) 6 127.0.0.1:6379> get test "123abc" 127.0.0.1:6379>
strlen
String length, returns the length of the data. If the key does not exist, it returns 0. Note that if the key value is an empty string, it also returns 0.
Syntax: STRLEN key
127.0.0.1:6379> get test "123abc" 127.0.0.1:6379> strlen test (integer) 6 127.0.0.1:6379> strlen tnt (integer) 0 127.0.0.1:6379> set tnt "" OK 127.0.0.1:6379> strlen tnt (integer) 0 127.0.0.1:6379> exists tnt (integer) 1 127.0.0.1:6379>
mset/mget
Set / get multiple key values at the same time
Syntax: MSET key value [key value...]
MGET key [key ...]
127.0.0.1:6379> flushall OK 127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> mset a 1 b 2 c 3 OK 127.0.0.1:6379> mget a b c 1) "1" 2) "2" 3) "3" 127.0.0.1:6379>
Section interview analysis
- How to realize the word count of blog? (strlen)
- How to continuously append the audit log to the specified key?(append)
- How do you implement a distributed self incrementing id? (incr snowflake algorithm)
- How to implement a blog like operation? (incr,decr)
Application practice of Hash type
Redis hash type is equivalent to HashMap in Java. Its implementation principle is consistent with HashMap. It is generally used to store object information and store the mapping of fields and field values. A hash type can contain up to 232-1 fields.
hset/hget
Grammatical structure
HSET key field value HGET key field HMSET key field value [field value...] HMGET key field [field] HGETALL key
HSET and HGET assignment and value
127.0.0.1:6379> hset user username chenchen (integer) 1 127.0.0.1:6379> hget user username "chenchen" 127.0.0.1:6379> hset user username chen (integer) 0 127.0.0.1:6379> keys user 1) "user" 127.0.0.1:6379> hgetall user 1) "username" 2) "chen" 127.0.0.1:6379> 127.0.0.1:6379> hset user age 18 (integer) 1 127.0.0.1:6379> hset user address "xi'an" (integer) 1 127.0.0.1:6379> hgetall user 1) "username" 2) "chen" 3) "age" 4) "18" 3) "address" 4) "xi'an" 127.0.0.1:6379>
The HSET command does not distinguish between insert and update operations. The HSET command returns 1 when performing insert operations and 0 when performing update operations.
hincrby
127.0.0.1:6379> hdecrby article total 1 #Error during execution 127.0.0.1:6379> hincrby article total -1 #There is no hdecrby subtraction command (integer) 1 127.0.0.1:6379> hget article total #Get value
hmset/hmget
HMSET and HMGET set and get object properties
127.0.0.1:6379> hmset person username tony age 18 OK 127.0.0.1:6379> hmget person age username 1) "18" 2) "tony" 127.0.0.1:6379> hgetall person 1) "username" 2) "tony" 3) "age" 4) "18" 127.0.0.1:6379>
Note: the above HMGET field order can be defined by yourself
hexists
Does the property exist 127.0.0.1:6379> hexists killer (error) ERR wrong number of arguments for 'hexists' command 127.0.0.1:6379> hexists killer a (integer) 0 127.0.0.1:6379> hexists user username (integer) 1 127.0.0.1:6379> hexists person age (integer) 1 127.0.0.1:6379>
hdel
Delete attribute
127.0.0.1:6379> hdel user age (integer) 1 127.0.0.1:6379> hgetall user 1) "username" 2) "chen" 127.0.0.1:6379> hgetall person 1) "username" 2) "tony" 3) "age" 4) "18" 127.0.0.1:6379>
hkeys/hvals
Only get the field name HKEYS or field value HVALS
127.0.0.1:6379> hkeys person 1) "username" 2) "age" 127.0.0.1:6379> hvals person 1) "tony" 2) "18" 2.3.8 hlen Number of elements 127.0.0.1:6379> hlen user (integer) 1 127.0.0.1:6379> hlen person (integer) 2 127.0.0.1:6379>
Section interview analysis
- Do you need to write memory to publish a blog? (required, hmset)
- What do you do when browsing blog content? (hmget)
- How to determine whether a blog exists? (hexists)
- How to delete a blog? (hdel)
- How do you store user information in a distributed system after you log in successfully? (hmset)
List type application practice
Redis's list type is equivalent to LinkedList in java, and its principle is a two-way linked list. It supports forward, reverse lookup, traversal and other operations, and the insertion and deletion speed is relatively fast. It is often used to realize the design of hot sales list, latest comments, etc.
lpush
Add a string element in the header of the list corresponding to the key
redis 127.0.0.1:6379> lpush mylist "world" (integer) 1 redis 127.0.0.1:6379> lpush mylist "hello" (integer) 2 redis 127.0.0.1:6379> lrange mylist 0 -1 1) "hello" 2) "world" redis 127.0.0.1:6379>
Redis Lrange returns the elements within the specified interval in the list, and the interval is specified by offset START and END. Where 0 represents the first element of the list, 1 represents the second element of the list, and so on. You can also use a negative subscript, - 1 for the last element of the list, - 2 for the penultimate element of the list, and so on
rpush
Add a string element at the end of the list corresponding to the key
redis 127.0.0.1:6379> rpush mylist2 "hello" (integer) 1 redis 127.0.0.1:6379> rpush mylist2 "world" (integer) 2 redis 127.0.0.1:6379> lrange mylist2 0 -1 1) "hello" 2) "world" redis 127.0.0.1:6379>
linsert
Add a string element before or after the specific position of the list corresponding to the key
redis 127.0.0.1:6379> rpush mylist3 "hello" (integer) 1 redis 127.0.0.1:6379> rpush mylist3 "world" (integer) 2 redis 127.0.0.1:6379> linsert mylist3 before "world" "there" (integer) 3 redis 127.0.0.1:6379> lrange mylist3 0 -1 1) "hello" 2) "there" 3) "world" redis 127.0.0.1:6379>
lset
Set the element value of the specified subscript in the list (generally used for modification)
redis 127.0.0.1:6379> rpush mylist4 "one" (integer) 1 redis 127.0.0.1:6379> rpush mylist4 "two" (integer) 2 redis 127.0.0.1:6379> rpush mylist4 "three" (integer) 3 redis 127.0.0.1:6379> lset mylist4 0 "four" OK redis 127.0.0.1:6379> lset mylist4 -2 "five" OK redis 127.0.0.1:6379> lrange mylist4 0 -1 1) "four" 2) "five" 3) "three" redis 127.0.0.1:6379>
lrem
Delete count elements with the same value from the list corresponding to the key. When count > 0, delete them from beginning to end
redis 127.0.0.1:6379> rpush mylist5 "hello" (integer) 1 redis 127.0.0.1:6379> rpush mylist5 "hello" (integer) 2 redis 127.0.0.1:6379> rpush mylist5 "foo" (integer) 3 redis 127.0.0.1:6379> rpush mylist5 "hello" (integer) 4 redis 127.0.0.1:6379> lrem mylist5 2 "hello" (integer) 2 redis 127.0.0.1:6379> lrange mylist5 0 -1 1) "foo" 2) "hello" redis 127.0.0.1:6379> count<0 When, delete from end to end redis 127.0.0.1:6379> rpush mylist6 "hello" (integer) 1 redis 127.0.0.1:6379> rpush mylist6 "hello" (integer) 2 redis 127.0.0.1:6379> rpush mylist6 "foo" (integer) 3 redis 127.0.0.1:6379> rpush mylist6 "hello" (integer) 4 redis 127.0.0.1:6379> lrem mylist6 -2 "hello" (integer) 2 redis 127.0.0.1:6379> lrange mylist6 0 -1 1) "hello" 2) "foo" redis 127.0.0.1:6379> count=0 Delete all when redis 127.0.0.1:6379> rpush mylist7 "hello" (integer) 1 redis 127.0.0.1:6379> rpush mylist7 "hello" (integer) 2 redis 127.0.0.1:6379> rpush mylist7 "foo" (integer) 3 redis 127.0.0.1:6379> rpush mylist7 "hello" (integer) 4 redis 127.0.0.1:6379> lrem mylist7 0 "hello" (integer) 3 redis 127.0.0.1:6379> lrange mylist7 0 -1 1) "foo" redis 127.0.0.1:6379>
ltrim
Keep the data within the value range of the specified key
redis 127.0.0.1:6379> rpush mylist8 "one" (integer) 1 redis 127.0.0.1:6379> rpush mylist8 "two" (integer) 2 redis 127.0.0.1:6379> rpush mylist8 "three" (integer) 3 redis 127.0.0.1:6379> rpush mylist8 "four" (integer) 4 redis 127.0.0.1:6379> ltrim mylist8 1 -1 OK redis 127.0.0.1:6379> lrange mylist8 0 -1 1) "two" 2) "three" 3) "four" redis 127.0.0.1:6379>
lpop
Delete the element from the header of the list and return the deleted element
redis 127.0.0.1:6379> lrange mylist 0 -1 1) "hello" 2) "world" redis 127.0.0.1:6379> lpop mylist "hello" redis 127.0.0.1:6379> lrange mylist 0 -1 1) "world" redis 127.0.0.1:6379>
rpop
Delete the element from the tail of the list and return the deleted element:
redis 127.0.0.1:6379> lrange mylist2 0 -1 1) "hello" 2) "world" redis 127.0.0.1:6379> rpop mylist2 "world" redis 127.0.0.1:6379> lrange mylist2 0 -1 1) "hello" redis 127.0.0.1:6379>
llen
Return the length of the list corresponding to the key:
redis 127.0.0.1:6379> llen mylist5 (integer) 2 redis 127.0.0.1:6379>
lindex
Return the element at the index position in the list with the name key:
redis 127.0.0.1:6379> lrange mylist5 0 -1 1) "three" 2) "foo" redis 127.0.0.1:6379> lindex mylist5 0 "three" redis 127.0.0.1:6379> lindex mylist5 1 "foo" redis 127.0.0.1:6379>
rpoplpush
Remove the element from the tail of the first list and add it to the head of the second list. Finally, return the value of the removed element. The whole operation is atomic If the first list is empty or does not exist, return nil:
rpoplpush lst1 lst1
rpoplpush lst1 lst2
Section interview analysis
- How to implement a queue structure based on redis? (lpush/rpop)
- How to implement a stack structure based on redis? (lpush/lpop)
- How to implement a blocking queue based on redis? (lpush/brpop)
- How to realize the fairness of second kill activities? (FIFO FIFO)
- Do you implement a message queue (sequence) through a list structure? (yes, FIFO - > lpush, rpop)
- How can the e-mail sending function during user registration improve its efficiency? (e-mail sending is to call three-party services. The bottom layer optimizes its efficiency through the queue. The queue is generally a list structure)
- How to dynamically update the sales list of goods? (those that sell well rank higher, linsert)
- What structure does the merchant's fan list use? (list structure)
Set type application practice
Redis's Set is similar to the HashSet in Java. It is an unordered Set of string type. Collection members are unique, which means that duplicate data cannot appear in the collection. The Set set in redis is implemented through hash table, so the complexity of adding, deleting and searching is O(1).
sadd
Add element, duplicate element failed to add, return 0
127.0.0.1:6379> sadd name tony (integer) 1 127.0.0.1:6379> sadd name hellen (integer) 1 127.0.0.1:6379> sadd name rose (integer) 1 127.0.0.1:6379> sadd name rose (integer) 0
smembers
Gets the member in the collection, for example
127.0.0.1:6379> smembers name
- "hellen"
- "rose"
- "tony"
spop
Removes and returns a random element in the collection
127.0.0.1:6379> smembers internet 1) "amoeba" 2) "redis" 3) "rabbitmq" 4) "nginx" 127.0.0.1:6379> spop internet "rabbitmq" 127.0.0.1:6379> spop internet "nginx" 127.0.0.1:6379> smembers internet 1) "amoeba" 2) "redis"
scard
Gets the number of members in the collection
127.0.0.1:6379> scard name (integer) 3
smove
Move an element to another collection
127.0.0.1:6379> sadd internet amoeba nginx redis (integer) 3 127.0.0.1:6379> sadd bigdata hadopp spark rabbitmq (integer) 3 127.0.0.1:6379> smembers internet 1) "amoeba" 2) "redis" 3) "nginx" 127.0.0.1:6379> smembers bigdata 1) "hadopp" 2) "spark" 3) "rabbitmq" 127.0.0.1:6379> smove bigdata internet rabbitmq (integer) 1 127.0.0.1:6379> smembers internet 1) "amoeba" 2) "redis" 3) "rabbitmq" 4) "nginx" 127.0.0.1:6379> smembers bigdata 1) "hadopp" 2) "spark" 127.0.0.1:6379>
sunion
Implement the union operation of a collection
127.0.0.1:6379> sunion internet bigdata 1) "redis" 2) "nginx" 3) "rabbitmq" 4) "amoeba" 5) "hadopp" 6) "spark"
Section interview analysis
- How do you realize the like function of the circle of friends? (sadd,srem,smembers,scard)
- How to implement a website voting statistics program?
- Do you know how to realize the attention in microblog?
Summary
This chapter mainly analyzes and practices the storage structures and basic operations of common data types in redis, and understands its application scenarios in combination with the characteristics of instructions,