当前位置:网站首页>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.
边栏推荐
- #22Map介绍与API
- 7-8 梯云纵
- 378. 骑士放置
- 7-3 最大子段和
- Paddledtx v1.0 has been released, and its security and flexibility have been comprehensively improved!
- 华为机器学习服务语音识别功能,让应用绘“声”绘色
- R language dplyr package group_ By function and summarize_ The at function calculates the dataframe to calculate the number of counts and the mean value of different groups (summary data by category v
- Use of laravel verifier
- SimpleDateFormat 格式化和解析日期的具体类
- [JS] - [string - application] - learning notes
猜你喜欢

从客户端到服务器

Helix distance of point

7-6 铺设油井管道

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

Blogs personal blog test point (manual test)
![[JS] - [array, Stack, queue, Link List basis] - Notes](/img/c6/a1bd3b8ef6476d7d549abcb442949a.png)
[JS] - [array, Stack, queue, Link List basis] - Notes

Super detailed cookie addition, deletion, modification and query

Fibonacci

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

【js】-【字符串-应用】- 学习笔记
随机推荐
Financial management [5]
Jetpack Compose 最新进展
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用AIC函数比较两个模型的AIC值的差异(简单模型和复杂模型)
Financial management [3]
Helix distance of point
当初吃土建起来的“中台”,现在为啥不香了?
文件包含漏洞问题
R language dplyr package group_ By function and summarize_ The at function calculates the dataframe to calculate the number of counts and the mean value of different groups (summary data by category v
golang convert map to json string
监听 Markdown 文件并热更新 Next.js 页面
golang convert json string to map
379. 捉迷藏
去处电脑桌面小箭头
[JS] - [stack, team - application] - learning notes
Blogs personal blog test point (manual test)
Building Survey [3]
7-7 求解众数问题
Basic data type
257. 关押罪犯
R language uses the aggregate function of epidisplay package to split numerical variables into different subsets based on factor variables, calculate the summary statistics of each subset, and customi