当前位置:网站首页>Yyds dry goods inventory tells us 16 common usage scenarios of redis at one go

Yyds dry goods inventory tells us 16 common usage scenarios of redis at one go

2022-06-24 23:27:00 Ma Nong Xiao Song

Catalog

  • cache
  • Data sharing distributed
  • Distributed lock
  • overall situation ID
  • Counter
  • Current limiting
  • Bit Statistics
  • The shopping cart
  • User message timeline timeline
  • Message queue
  • Luck draw
  • give the thumbs-up 、 Sign in 、 Clock in
  • Product label
  • Product selection
  • User attention 、 Recommended model
  • Ranking List

1、 cache

String Types such as : Hot data cache ( For example, report 、 Star cheating ), Object caching 、 Full page caching 、 Can improve access to hotspot data .

2、 Data sharing distributed

String type , because Redis It's a distributed, stand-alone service , Can be shared between multiple applications, for example : Distributed Session

      
      
< dependency >
< groupId >org.springframework.session </ groupId >
< artifactId >spring-session-data-redis </ artifactId >
</ dependency >
  • 1.
  • 2.
  • 3.
  • 4.

3、 Distributed lock

String type setnx Method , Only when it doesn't exist can it be added successfully , return 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);
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

4、 overall situation ID

int type ,incrby, Using atomicity

incrby userid 1000

The scene of sub database and sub table , Take one piece at a time

5、 Counter

int type ,incr Method

for example : The amount of reading 、 Microblog likes 、 Allow a certain delay , Write... First Redis Then synchronize to the database regularly

6、 Current limiting

int type ,incr Method takes the visitor's ip And other information as key, Add one count at a time , If it exceeds the number of times, it will return false

7、 Bit Statistics

String Type of bitcount(1.6.6 Of bitmap Data structure introduction )

The characters are written with 8 Bit binary storage

      
      
set k1 a
setbit k1 6 1
setbit k1 7 0
get k1
/* 6 7 Representative a Modification of binary bits
a Corresponding ASCII Code is 97, Converting to binary data is 01100001
b Corresponding ASCII Code is 98, Converting to binary data is 01100010

because bit Very space saving (1 MB=8388608 bit), It can be used for statistics of large amount of data .
*/
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

for example : Online user statistics , Retain user statistics

      
      
setbit onlineusers 01
setbit onlineusers 11
setbit onlineusers 20
  • 1.
  • 2.
  • 3.

Support bitwise and 、 Bitwise OR etc

      
      
BITOPANDdestkeykey[key...] , To one or more key Seek logic and , And save the result to destkey .
BITOPORdestkeykey[key...] , To one or more key Seek logic or , And save the result to destkey .
BITOPXORdestkeykey[key...] , To one or more key Seek logical XOR , And save the result to destkey .
BITOPNOTdestkeykey , For given key Logic is not , And save the result to destkey .
  • 1.
  • 2.
  • 3.
  • 4.

To calculate the 7 Tiandu online users

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

8、 The shopping cart

String or hash. all String What can be done hash You can do it. #yyds Dry inventory # Say in one breath Redis 16 Three common use scenarios _redis

  • key: user id;field: goods id;value: The number .
  • +1:hincr.-1:hdecr. Delete :hdel. Future generations :hgetall. Number of goods :hlen.

9、 User message timeline timeline

list, Double linked list , Act directly as timeline Just fine . Insert in order

10、 Message queue

List Two blocked pop-up operations are provided :blpop/brpop, Timeout can be set

  • blpop:blpop key1 timeout Remove and get the first element of the list , If there are no elements in the list, it will block the list until the wait timeout or pop-up elements are found .
  • brpop:brpop key1 timeout Remove and get the last element of the list , If there are no elements in the list, it will block the list until the wait timeout or pop-up elements are found .

Operation above . In fact, that is java Blocking queue . The more you learn . The lower the cost of learning

  • queue : First in, first out :rpush blpop, Left head right tail , Right into the queue , Queue left
  • Stack : First in, then out :rpush brpop

11、 Luck draw

With a random value

      
      
spop myset
  • 1.

12、 give the thumbs-up 、 Sign in 、 Clock in

#yyds Dry inventory # Say in one breath Redis 16 Three common use scenarios _iphone_02 If the microblog above ID yes t1001, user ID yes u3001 use like:t1001 To maintain the t1001 All the like users of this microblog

  • I like this microblog :sadd like:t1001 u3001
  • Cancel likes :srem like:t1001 u3001
  • Do you like it :sismember like:t1001 u3001
  • All users who like :smembers like:t1001
  • Number of likes :scard like:t1001

Is it much simpler than the database .

13、 Product label

#yyds Dry inventory # Say in one breath Redis 16 Three common use scenarios _redis_03 Old rules , use tags:i5001 To maintain all labels on the product .

  • sadd tags:i5001 The picture is clear and delicate
  • sadd tags:i5001 True color clear screen
  • sadd tags:i5001 The process is extremely

14、 Product selection

      
      
// Get the difference set
sdiff set1 set2
// Get intersection (intersection )
sinter set1 set2
// Get Union
sunion set1 set2
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

#yyds Dry inventory # Say in one breath Redis 16 Three common use scenarios _redis_04 If :iPhone11 Listed on the

      
      
sadd brand:apple iPhone11

sadd brand:ios iPhone11

sad screensize:6.0-6.24 iPhone11

sad screentype:lcd iPhone 11
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

Competition goods , Apple 、ios Of 、 Screen in 6.0-6.24 Between , The screen material is LCD The screen

      
      
sinter brand:apple brand:ios screensize:6.0-6.24 screentype:lcd
  • 1.

15、 User attention 、 Recommended model

follow Focus on fans Fans pay attention to each other :

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

The people I'm concerned about are paying attention to him ( intersect ):

  • sinter 1:follow 2:fans

People you may know :

  • user 1 People you may know ( Difference set ):sdiff 2:follow 1:follow
  • user 2 People you may know :sdiff 1:follow 2:follow

16、 Ranking List

id by 6001 The number of news hits plus 1:

      
      
zincrby hotNews:20190926 1 n6001
  • 1.

Get the most hits today 15 strip :

      
      
zrevrange hotNews:20190926 0 15 withscores
  • 1.

#yyds Dry inventory # Say in one breath Redis 16 Three common use scenarios _ cache _05

      
      
Address :https://blog.csdn.net/weixin_43878826/article/details/119461093
  • 1.


原网站

版权声明
本文为[Ma Nong Xiao Song]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211127044012.html