Common application scenarios of redis


String store

  1. Click count function, incr key:001 Self increasing, such as link hits
  2. Distributed lock set key value NX PX 1000 / / set random value and set expiration time through setnx. If the value is written successfully, the lock is added successfully.
  3. Session sharing in cluster environment

List store

Often used in queues, stacks, can be found according to the index subscript or range of queries.
WeChat official account subscription list:
Xiaoming useId:001 Concerned about Alibaba recruitment and Tencent recruitment official account.
1. Alibaba first sent a push 001: lpush userId:001 :gongzhonghao 001
2. Tencent sent another push 002: lpush userId:001 :gongzhonghao 002;
Three latest subscription messages are displayed: lrange userId:001 :gongzhonghao 0 2

Hash storage

Design of shopping cart
Common application scenarios of redis
1. Display all shopping cart items
2. Total number of items in shopping cart
3. Increase and decrease the quantity of goods
4. Remove goods
5. Settlement price – directly calculated by page parameters

Add to Cart: hmset user01: cart001 goods1id 1 goods2id 2
Display is to get all products and quantities of hash: hgetall user01: cart001

Set store

1: The app has a lottery activity with the activity ID of 0001. How to implement the lottery based on redis?

For example, four people participated in the lottery.
1. Add userid set:sadd act :0001 001 002 003 004;
2. Check which lottery users are: smembers act:0001
3. Randomly draw 2 people: srandmember act:0001 Return 2 IDS; exclude spop if you have won the prize act:0001 2 , indicating that two are randomly selected and removed from the set.
Note: some versions do not support spop act:0001 count Such an order.

2: How to design “like” on Weibo or “like” on friends?
Yi Jianlian posted a micro blog weibo:001 ,
1. Fan a likes: Sadd weibo:001 fensiA
2. Check whether user B has liked: sismember weibo:001 yonghuB , returns 1, otherwise 0.
3. Fan a: SREM weibo:001 fensiA–rem I.e. remove
4. View the list of users liked by this Weibo: smembers weibo:001
5. View total likes: Scar weibo:001

3、 How to design micro relationship of microblog? Common concern? People I care about also care about him?
1. These are the people I pay attention to: Sadd mecare Kobe yijianlian James
2. These are the people James pays attention to: Sadd James care Kobe yijianlian Yaoming
3. People concerned by Yaoming: Sadd yaomingcare James curry rose
4. The people that Jemes and me pay attention to together are the intersection: single mecare yaomingcare – > return to James, and the intersection is the intersection
5. People I care about also pay attention to him (Kobe), and people I care about James also pay attention to Kobe,
sismember meCare kobe–sismember jamesCare kobe
6. People I may know
Sdiff mecare yaomingcare / / calculate difference set

Zset ordered set

Often used in leaderboards, news topic lists, etc.
Design and implementation of search hotspot on Baidu page, with date as keyCommon application scenarios of redis

zadd hot:20191026 1 Qingyang found a recurrence of a waist injury in a boy following a woman in Dalian

1. Click hot links + 1, zincrby hot:20191026 1 Discovery of large oil field in Qingyang

2. Ranking implementation, zrevrange hot:20191026 0 7 withcore — returns the members in the specified range 0-7 of the ordered set. Through the index, the score is from high to low
3. Display the top eight in the recent three days
zrevrange hot:20191024-hot:20191026 0 7 withscore。
There are other command applications that you can query by yourself~~