Detailed explanation of the application of the five traditional data types of Redis interviews

Prerequisite : install redis6.0.8
Redis official website: https://redis.io/
Redis Chinese website: http://www.redis.cn/

Basic types of redis:

  1. string
  2. list
  3. set
  4. zset(sorted set)
  5. hash

Note 1 : The command is not case sensitive, but the key is case sensitive.

127.0.0.1:6379> set K1 V1
OK
127.0.0.1:6379> set k1 v2
OK
127.0.0.1:6379> get K1
"V1"
127.0.0.1:6379> get k1
"v2"
127.0.0.1:6379>

The values ​​of K1 and k1 are different.

Note 2 : help @+type noun, query command. For example, help @list

127.0.0.1:6379> help @list

  BLPOP key [key ...] timeout
  summary: Remove and get the first element in a list, or block until one is available
  since: 2.0.0

  BRPOP key [key ...] timeout
  summary: Remove and get the last element in a list, or block until one is available
  since: 2.0.0

  BRPOPLPUSH source destination timeout
  summary: Pop a value from a list, push it to another list and return it; or block until one is available
  since: 2.2.0

  LINDEX key index
  summary: Get an element from a list by its index
  since: 1.0.0

  LINSERT key BEFORE|AFTER pivot value
  summary: Insert an element before or after another element in a list
  since: 2.2.0

  LLEN key
  summary: Get the length of a list
  since: 1.0.0

  LPOP key
  summary: Remove and get the first element in a list
  since: 1.0.0

1. What is the usage scenario of string type?

General usage:
SET key value
GET key

127.0.0.1:6379> set k1 ct1
OK
127.0.0.1:6379> set k2 ct2
OK
127.0.0.1:6379> get k1
"ct1"

Set/get multiple key values ​​at the same time

MSET key value [key value…]
MGET key [key…]

127.0.0.1:6379> mset k3 ct3 k4 ct4
OK
127.0.0.1:6379> mget k3 k4
1) "ct3"
2) "ct4"

Increase or decrease the value
1. Increment the number: INCR key (you do not need to set the value of the key in advance. If the key is set in advance but the value is not a number, an error will be reported)

127.0.0.1:6379> incr d1
(integer) 1
127.0.0.1:6379> incr d1
(integer) 2
127.0.0.1:6379> incr d1
(integer) 3
127.0.0.1:6379> incr d1
(integer) 4
127.0.0.1:6379>

Increase the specified integer: INCRBY key increment

127.0.0.1:6379> incrby d1 2
(integer) 6
127.0.0.1:6379> incrby d1 2
(integer) 8

Decrement value: DECR key

127.0.0.1:6379> decr d1
(integer) 7
127.0.0.1:6379> decr d1
(integer) 6
127.0.0.1:6379> decr d1
(integer) 5

Decrease the specified integer: DECRBY key decrement

127.0.0.1:6379> decrby d1 2
(integer) 3
127.0.0.1:6379> decrby d1 2
(integer) 1

Get string length: STRLEN key

127.0.0.1:6379> strlen d1
(integer) 1

Distributed lock

SETNX key value
SET key value [EX seconds] [PX milliseconds] [NX|XX]
EX: How many seconds after the
key expires PX: How many milliseconds after the
key expires NX: When the key does not exist, create the key, the effect is equivalent In setnx
XX: when the key exists, overwrite the key

127.0.0.1:6379> set k1 v1 EX 20
OK
127.0.0.1:6379> ttl k1
(integer) 17
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> ttl k1
(integer) 11
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> ttl k1
(integer) 6
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> ttl k1
(integer) -2

Application scenario
If an e-commerce website, a certain product, people who like the product will like it, and those who don't like it, they will like it, and record the number of likes and dislikes respectively.
There are also if the number of likes of the blog is published, the data of the articles in the collection, the product number, the generation of the order number, and so on.
We can all write like this

127.0.0.1:6379> incr items:10
(integer) 1
127.0.0.1:6379> incr items:10
(integer) 2
127.0.0.1:6379> incr items:10
(integer) 3
127.0.0.1:6379> incr items:10
(integer) 4
127.0.0.1:6379> get items:10
"4"

2. What is the usage scenario of hash type?

The Hash type of Redis is equivalent to Map<String, Map<key, value>> in Java

Set one field value at a time HSET key field value

127.0.0.1:6379> hset user id 110
(integer) 1
127.0.0.1:6379> hset user name ct
(integer) 1

Get one field value at a time HGET key field

127.0.0.1:6379> hget user id
"110"
127.0.0.1:6379> hget user name
"ct"
127.0.0.1:6379>

Set multiple field values ​​at once HMSET key field value [field value …]

127.0.0.1:6379> hmset user age 18 sex man
OK

Get multiple field values ​​at once HMGET key field [field …]

127.0.0.1:6379> hmget user age sex
1) "18"
2) "man"

Get all field values ​​HGETALL key

127.0.0.1:6379> hgetall user
1) "id"
2) "110"
3) "name"
4) "ct"
5) "age"
6) "18"
7) "sex"
8) "man"

Get all the quantity HLEN in a key

127.0.0.1:6379> hlen user
(integer) 4

Delete a key HDEL

127.0.0.1:6379> hdel user age
(integer) 1
127.0.0.1:6379> hgetall user
1) "id"
2) "110"
3) "name"
4) "ct"
5) "sex"
6) "man"

Application scenarios:
shopping carts in e-commerce projects, ordering food, etc.

Shopping cart number: shopcar:01

New product: shop001

127.0.0.1:6379> hset shopcar:01 shop001 1
(integer) 1

New product: shop002

127.0.0.1:6379> hset shopcar:01 shop002 1
(integer) 1

Increase the number of products: shop001

127.0.0.1:6379> hincrby shopcar:01 shop001 1
(integer) 2

Total number of products

127.0.0.1:6379> hlen shopcar:01
(integer) 2

View shopping cart information

127.0.0.1:6379> hgetall shopcar:01
1) "shop001"
2) "2"
3) "shop002"
4) "1"

3. List type usage scenarios

Add the element LPUSH key value [value …] to the left of the list

127.0.0.1:6379> lpush list1 1 2 2 3 3 4 5
(integer) 7
127.0.0.1:6379> lrange list1 0 -1 //全部遍历
1) "5"
2) "4"
3) "3"
4) "3"
5) "2"
6) "2"
7) "1"

Orderly with repetition

Add the element RPUSH key value [value …] to the right of the list

127.0.0.1:6379> rpush list1 6
(integer) 8
127.0.0.1:6379> rpush list1 7
(integer) 9
127.0.0.1:6379> rpush list1 8
(integer) 10
127.0.0.1:6379> lrange list1 0 -1
 1) "5"
 2) "4"
 3) "3"
 4) "3"
 5) "2"
 6) "2"
 7) "1"
 8) "6"
 9) "7"
10) "8"

View the list LRANGE key start stop

lrange list1 0 -1 //全部遍历

Get the number of elements in the list LLEN key

127.0.0.1:6379> llen list1
(integer) 10

Application scenario:
WeChat article subscription official account: one person can subscribe to multiple official accounts. If the user [likeauthor:L001] subscribes to [11] and [22] author’s articles respectively, put [11] and [22] in User【likeauthor:L001】list, and then show it to users.

127.0.0.1:6379> lpush likeauthor:L001 11 22
(integer) 2
127.0.0.1:6379> lrange likeauthor:L001 0 -1
1) "22"
2) "11"

4. Set type usage scenarios

Disorder and no repetition

Add element SADD key member [member …]

127.0.0.1:6379> sadd set1 2 3 3 5 4 1 5
(integer) 5
127.0.0.1:6379> smembers set1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"

Delete element SREM key member [member …]

127.0.0.1:6379> srem set1 5
(integer) 1
127.0.0.1:6379> smembers set1
1) "1"
2) "2"
3) "3"
4) "4"

Get all the elements in the collection SMEMBERS key

127.0.0.1:6379> smembers set1
1) "1"
2) "2"
3) "3"
4) "4"

Determine whether the element is in the collection SISMEMBER key member

127.0.0.1:6379> sismember set1 2
(integer) 1

Get the number of elements in the collection SCARD key

127.0.0.1:6379> scard set1
(integer) 4

An element is randomly popped from the set, and the element is not deleted. SRANDMEMBER key [number]

127.0.0.1:6379> srandmember set1//不写默认是1个,写几个就是几个
"4"
127.0.0.1:6379> srandmember set1
"2"
127.0.0.1:6379> scard set1
(integer) 4

Randomly pop an element from the set, and delete one SPOP key[number]

127.0.0.1:6379> spop set1 //不写默认是1个,写几个就是几个
"1"
127.0.0.1:6379> scard set1
(integer) 3

Set operations

Set difference operation A-
A set
consisting of elements that belong to A but not to B SDIFF key [key …]

127.0.0.1:6379> sadd set1 1 2 3
(integer) 3
127.0.0.1:6379> sadd set2 2 3 4
(integer) 3
127.0.0.1:6379> sdiff set1 set2
1) "1"

Set intersection operation A ∩ B
belongs to the set consisting of the common elements of A and B.
SINTER key [key …]

127.0.0.1:6379> sinter set1 set2
1) "2"
2) "3"

The set union operation AUB
belongs to A or the set after the elements belonging to B are merged.
SUNION key [key …]

127.0.0.1:6379> sunion set1 set2
1) "1"
2) "2"
3) "3"
4) "4"

Application scenario 1:
WeChat mini-program draws, publish a draw, click [Participate Now], the current user ID will be stored in the set, and then use the spop command

10 people, 3 second prizes, 1 first prize

127.0.0.1:6379> sadd set3 1 2 3 4 5 6 7 8 9 10
(integer) 10
127.0.0.1:6379> spop set3 3
1) "5"
2) "7"
3) "10"
127.0.0.1:6379> spop set3 1
1) "1"
127.0.0.1:6379> smembers set3
1) "2"
2) "3"
3) "4"
4) "6"
5) "8"
6) "9"

Application Scenario 2:
Like in WeChat Moments, post a Moments, after a long time like it, put the friend id in the set

127.0.0.1:6379> sadd message:01 user1 user2
(integer) 2
127.0.0.1:6379> smembers message:01
1) "user2"
2) "user1"

Unlike

127.0.0.1:6379> srem message:01 user1
(integer) 1
127.0.0.1:6379> smembers message:01
1) "user2"

Show users who have liked

127.0.0.1:6379> smembers message:01
1) "user2"

Like user statistics are common red numbers

127.0.0.1:6379> scard message:01
(integer) 1

Judge whether a friend has liked you

127.0.0.1:6379> sismember message:01 user3
(integer) 0

Application Scenario 3: People
Followed by Friends on Weibo

127.0.0.1:6379> sadd s1 1 2 3 4 5
(integer) 5
127.0.0.1:6379> sadd s2 3 4 5 6 7
(integer) 5
127.0.0.1:6379> sinter s1 s2
1) "3"
2) "4"
3) "5"

The people I follow also pay attention to it (everybody has the same hobby)

127.0.0.1:6379> sadd s1 1 2 3 4 5
(integer) 5
127.0.0.1:6379> sadd s2 3 4 5 6 7
(integer) 5
127.0.0.1:6379> sismember s1 3
(integer) 1
127.0.0.1:6379> sismember s2 3
(integer) 1

QQ internal push, people you may know

127.0.0.1:6379> sadd s1 1 2 3 4 5
(integer) 5
127.0.0.1:6379> sadd s2 3 4 5 6 7
(integer) 5
127.0.0.1:6379> sdiff s1 s2
1) "1"
2) "2"
127.0.0.1:6379> sdiff s2 s1
1) "6"
2) "7"

5.zset type usage scenarios

Add an element and the element's score to the ordered set

Add element ZADD key score member [score member …]

127.0.0.1:6379> zadd zset1 100 mov1 80 mov2
(integer) 2

Return all elements between index start and stop in the order of element scores from small to large ZRANGE key start stop [WITHSCORES]

127.0.0.1:6379> zrange zset1 0 -1
1) "mov2"
2) "mov1"

Get the score of the element ZSCORE key member

127.0.0.1:6379> zscore zset1 mov1
"100"

Delete element ZREM key member [member …]

127.0.0.1:6379> zrem zset1 mov1
(integer) 1
127.0.0.1:6379> zrange zset1 0 -1
1) "mov2"

Get the elements of the specified score range ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]

127.0.0.1:6379> zrange zset1 0 -1
1) "mov2"
127.0.0.1:6379> zadd zset1 70 mov3
(integer) 1
127.0.0.1:6379> zadd zset1 60 mov4
(integer) 1
127.0.0.1:6379> zadd zset1 50 mov5
(integer) 1
127.0.0.1:6379> ZRANGEBYSCORE zset1 60 70
1) "mov4"
2) "mov3"

Increase the score of an element ZINCRBY key increment member

127.0.0.1:6379> ZINCRBY zset1 5 mov5
"55"

Get the number of elements in the collection ZCARD key

127.0.0.1:6379> zcard zset1
(integer) 4

Get the number of elements in the specified score range ZCOUNT key min max

127.0.0.1:6379> zcount zset1 55 70
(integer) 3

Delete elements according to the ranking range ZREMRANGEBYRANK key start stop

127.0.0.1:6379> ZREMRANGEBYRANK zset1 3 4
(integer) 1
127.0.0.1:6379> ZRANGE zset1 0 -1
1) "mov5"
2) "mov4"
3) "mov3"

Get the ranking of the element

ZRANK key member from small to large

127.0.0.1:6379> ZRANK zset1 mov3//索引从0开始的
(integer) 2

ZREVRANK key member from big to small

127.0.0.1:6379> ZREVRANK zset1 mov3//索引从0开始的
(integer) 0

Application scenarios:
1. Define the product sales rankings, the key is goods, and the score is the sales volume.
Product number 1001 sales volume 9, product number 1002 sales 15

127.0.0.1:6379> zadd goods 9 1001 15 1002
(integer) 2

Customer bought 2 pieces of 1001, sales volume +2

127.0.0.1:6379> zincrby goods 2 1001
"11"

Seeking the top 10 products by sales volume

127.0.0.1:6379> zrange goods 0 9 withscores
1) "1001"
2) "11"
3) "1002"
4) "15"

2. Tik Tok Hot Search
Click on the video

127.0.0.1:6379> ZINCRBY hotvcr 1 mov1
"1"
127.0.0.1:6379> ZINCRBY hotvcr 1 mov2
"1"
127.0.0.1:6379> ZINCRBY hotvcr 5 mov1
"6"
127.0.0.1:6379> ZINCRBY hotvcr 11 mov2
"12"

Show the top 10 rankings of the day

127.0.0.1:6379> ZREVRANGE hotvcr 0 9 withscores
1) "mov2"
2) "12"
3) "mov1"
4) "6"