当前位置:网站首页>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. 
- 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
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
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.
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.

Address :https://blog.csdn.net/weixin_43878826/article/details/119461093
- 1.
边栏推荐
- . Net 7 Preview 1 has been officially released
- 15 lines of code using mathematical formulas in wangeditor V5
- Building Survey [2]
- RT thread uses RT kprintf
- OpenSSL SSL_read: Connection was reset, errno 10054
- Listen to the markdown file and hot update next JS page
- Ningde times will increase RMB 45billion: Hillhouse subscribes RMB 3billion and Zeng Yuqun still controls 23% of the equity
- Building Survey [3]
- R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用summary函数获取模型汇总统计信息、解读模型系数交互作用及其显著性
- Whereabouts computer desktop small arrow
猜你喜欢

7-6 铺设油井管道

当初吃土建起来的“中台”,现在为啥不香了?

EMI的主要原因-工模电流

【js】-【數組、棧、隊列、鏈錶基礎】-筆記

Detailed explanation of online group chat and dating platform project (servlet implementation)

Latest development of jetpack compose

【js】-【树】-学习笔记

Chapter VI skills related to e-learning 5 (super parameter verification)

InnoDB, the storage engine of MySQL Architecture Principle_ Redo log and binlog

Installing IBM CPLEX academic edition | CONDA installing CPLEX
随机推荐
376. 機器任務
Construction equipment [6]
QT to place the form in the lower right corner of the desktop
OpenSSL SSL_read: Connection was reset, errno 10054
Online group chat and dating platform test point
点的螺旋距离
Concurrent shared model management
华为机器学习服务语音识别功能,让应用绘“声”绘色
Pseudo original intelligent rewriting API Baidu - good collection
golang map clear
How should we measure agile R & D projects?
UNION ALL UNION FULL JOIN
SQL -convert function
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用summary函数获取模型汇总统计信息、解读模型系数交互作用及其显著性
Mycms we media CMS V3.0, resource push optimization, new free template
378. 骑士放置
Selection (025) - what is the output of the following code?
#22Map介绍与API
InnoDB, the storage engine of MySQL Architecture Principle_ Redo log and binlog
Blogs personal blog project details (servlet implementation)