当前位置:网站首页>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.
边栏推荐
- 第六章 网络学习相关技巧5(超参数验证)
- Online group chat and dating platform test point
- How should we measure agile R & D projects?
- R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用summary函数获取模型汇总统计信息、解读模型系数交互作用及其显著性
- jar中没有主清单属性
- Fibonacci
- SimpleDateFormat 格式化和解析日期的具体类
- R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用AIC函数比较两个模型的AIC值的差异(简单模型和复杂模型)
- 376. 机器任务
- Jetpack Compose 最新进展
猜你喜欢
【js】-【數組、棧、隊列、鏈錶基礎】-筆記
[JS] - [array, Stack, queue, Link List basis] - Notes
How should we measure agile R & D projects?
Jetpack Compose 最新进展
[basic knowledge] ~ half adder & full adder
Online group chat and dating platform test point
Pseudo original intelligent rewriting API Baidu - good collection
Paddledtx v1.0 has been released, and its security and flexibility have been comprehensively improved!
文件包含漏洞问题
当初吃土建起来的“中台”,现在为啥不香了?
随机推荐
Helix distance of point
企业数据防泄露解决方案分享
golang convert map to json string
7-6 铺设油井管道
7-7 数字三角形
QT to place the form in the lower right corner of the desktop
File contains vulnerability issues
Actipro WPF Controls 2022.1.2
Ganglia 的安装与部署
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses exp function and coef function to obtain the corresponding odds rat
[JS] - [linked list - application] - learning notes
Idea creation module prompt already exists
257. detention of offenders
选择类排序法
R语言使用MatchIt包进行倾向性匹配分析、使用match.data函数构建匹配后的样本集合、对匹配后的样本的不同分组对应的目标变量的均值进行Welch双样本t检验分析、双独立样本t检验
7-3 最大子段和
Docker-mysql8-master-slave
R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用exp函数、confint函数、coef函数获取模型中每个变量(自变量改变一个单位)对应的优势比的置信区间
Bubble sort
R语言使用epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息、自定义FUN参数为多个统计量函数名称的列表计算多个统计量