Home  >  Article  >  Database  >  Where can Redis be used? 16 common usage scenarios shared

Where can Redis be used? 16 common usage scenarios shared

青灯夜游
青灯夜游forward
2021-09-18 20:01:303379browse

Where can Redis be applied? This article will share with you 16 common usage scenarios of Redis in one go. I hope it will be helpful to everyone!

Where can Redis be used? 16 common usage scenarios shared

[Related recommendations: Redis video tutorial]

1. Cache

String type

For example: hot data cache (such as reports, celebrity cheating), object cache, full page cache, access data that can improve hot data.

Where can Redis be used? 16 common usage scenarios shared

2. Distributed data sharing

String type, because Redis is a distributed independent service and can be shared between multiple applications

For example: Distributed Session

<dependency> 
 <groupId>org.springframework.session</groupId> 
 <artifactId>spring-session-data-redis</artifactId> 
</dependency>

3, Distributed lock

String type setnx method can only be added successfully if it does not exist, and returns true

public static boolean getLock(String key) {
    Long flag = jedis.setnx(key, "1");
    if (flag == 1) {
        jedis.expire(key, 10);
    }
    return flag == 1;
}

public static void releaseLock(String key) {
    jedis.del(key);
}

4, Global ID

int type, incrby, using atomicity

incrby userid 1000

In the scenario of sub-database and sub-table, get a section at one time

5, Counter

int type, incr method

For example: the number of articles read, the number of Weibo likes, allow a certain delay, first write to Redis and then synchronize to the database regularly

6. Current limit

int type, incr method

uses the visitor's IP and other information as the key. Each visit increases the count. If the number exceeds the number, false is returned

7. Bit statistics

Bitcount of String type (bitmap data structure introduction in 1.6.6)

characters are stored in 8-bit binary

set k1 a
setbit k1 6 1
setbit k1 7 0
get k1 
/* 6 7 代表的a的二进制位的修改
a 对应的ASCII码是97,转换为二进制数据是01100001
b 对应的ASCII码是98,转换为二进制数据是01100010

因为bit非常节省空间(1 MB=8388608 bit),可以用来做大数据量的统计。
*/

For example: online user statistics , retain user statistics

setbit onlineusers 01 
setbit onlineusers 11 
setbit onlineusers 20

Support bitwise AND, bitwise OR, etc. operations

BITOPANDdestkeykey[key...] ,对一个或多个 key 求逻辑并,并将结果保存到 destkey 。       
BITOPORdestkeykey[key...] ,对一个或多个 key 求逻辑或,并将结果保存到 destkey 。 
BITOPXORdestkeykey[key...] ,对一个或多个 key 求逻辑异或,并将结果保存到 destkey 。 
BITOPNOTdestkeykey ,对给定 key 求逻辑非,并将结果保存到 destkey 。

Calculate users who have been online for 7 days

BITOP "AND" "7_days_both_online_users" "day_1_online_users" "day_2_online_users" ...  "day_7_online_users"

8. Shopping cart

String or hash. All hashes that can be done with String can be done

Where can Redis be used? 16 common usage scenarios shared

  • key: user id; field: product id; value: product quantity.
  • 1: hincr. -1:hdecr. Delete:hdel. Select all: hgetall. Number of items: hlen.

9. User message timeline timeline

list, a doubly linked list, can be used directly as timeline. Insertion order

10. Message queue

List provides two blocking pop-up operations: blpop/brpop, and the timeout can be set

  • blpop: blpop key1 timeout removes and gets the first element of the list. If there is no element in the list, the list will be blocked until the wait times out or a pop-up element is found.
  • brpop: brpop key1 timeout removes and gets the last element of the list. If there are no elements in the list, the list will be blocked until the wait times out or a pop-up element is found.

The above operation. In fact, it is Java's blocking queue. The more things you learn. The lower the learning cost

  • Queue: first in, last out: rpush blpop, left head and right tail, right side enters the queue, left side exits the queue
  • Stack: first in, last out: rpush brpop

11. Draw

Comes with a random value

spop myset

12. Like, sign in, clock in

Where can Redis be used? 16 common usage scenarios shared

Suppose the above Weibo ID is t1001 and the user ID is u3001

Use like:t1001 to maintain t1001. All users who liked this Weibo

  • liked this Weibo: sadd like:t1001 u3001
  • Cancel like: srem like:t1001 u3001
  • Like or not: sismember like:t1001 u3001
  • All users who like it :smembers like:t1001
  • Likes: scard like:t1001

Isn’t it much simpler than the database?

13. Product tags

Where can Redis be used? 16 common usage scenarios shared

The old rule is to use tags:i5001 to maintain all tags of the product.

  • sadd tags:i5001 The picture is clear and delicate
  • sadd tags:i5001 True color clear display
  • sadd tags:i5001 The process is superb

14. Product selection

// 获取差集
sdiff set1 set2
// 获取交集(intersection )
sinter set1 set2
// 获取并集
sunion set1 set2

Where can Redis be used? 16 common usage scenarios shared

If: iPhone11 is on the market

sadd brand:apple iPhone11

sadd brand:ios iPhone11

sad screensize:6.0-6.24 iPhone11

sad screentype:lcd iPhone 11

Select products, Apple, ios, screen between 6.0-6.24 Sometimes, the screen material is LCD screen

sinter brand:apple brand:ios screensize:6.0-6.24 screentype:lcd

15. User attention, recommended models

follow Follow fans Fans

Follow each other:

  • sadd 1:follow 2
  • sadd 2:fans 1
  • sadd 1:fans 2
  • sadd 2:follow 1

I follow People also followed him (take intersection):

  • sinter 1:follow 2:fans

People you may know:

  • 用户1可能认识的人(差集):sdiff 2:follow 1:follow
  • 用户2可能认识的人:sdiff 1:follow 2:follow

16、排行榜

id 为6001 的新闻点击数加1:

zincrby hotNews:20190926 1 n6001

获取今天点击最多的15条:

zrevrange hotNews:20190926 0 15 withscores

Where can Redis be used? 16 common usage scenarios shared

Redis 用的好,加薪少不了

原文地址:https://juejin.cn/post/6994229128534687781

作者:码猿技术专栏

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of Where can Redis be used? 16 common usage scenarios shared. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:掘金--码猿技术专栏. If there is any infringement, please contact admin@php.cn delete