当前位置:网站首页>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.
边栏推荐
- The R language uses the matchit package for propensity matching analysis and match The data function constructs the matched sample set, and performs Welch double sample t-test analysis and double inde
- 379. 捉迷藏
- How should we measure agile R & D projects?
- Notes for laravel model
- Financial management [2]
- 冒泡排序
- 第六章 网络学习相关技巧5(超参数验证)
- Bubble sort
- Chapter VI skills related to e-learning 5 (super parameter verification)
- OpenSSL SSL_read: Connection was reset, errno 10054
猜你喜欢

Latest development of jetpack compose

MySQL 表的增删查改

Jetpack Compose 最新进展
![[JS] - [array application] - learning notes](/img/8a/808fde0cc86e0ec5e1f5558ba196b4.png)
[JS] - [array application] - learning notes

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

7-6 铺设油井管道

第六章 网络学习相关技巧5(超参数验证)

Ganglia 的安装与部署

Mousse shares listed on Shenzhen Stock Exchange: becoming popular by mattress and "foreign old man", with a market value of 22.4 billion yuan

Simpledateformat concrete classes for formatting and parsing dates
随机推荐
監聽 Markdown 文件並熱更新 Next.js 頁面
【js】-【数组、栈、队列、链表基础】-笔记
[JS] - [string - application] - learning notes
Financial management [1]
Financial management [3]
[JS] - [tree] - learning notes
Financial management [2]
【js】-【树】-学习笔记
慕思股份深交所上市:靠床垫和“洋老头”走红 市值224亿
冒泡排序
379. hide and seek
R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用exp函数、confint函数、coef函数获取模型中每个变量(自变量改变一个单位)对应的优势比的置信区间
376. machine tasks
[JS] - [linked list - application] - learning notes
Quickly build KVM virtual machine on # yyds dry goods inventory # physical machine
企业数据防泄露解决方案分享
378. Knight placement
Idea creation module prompt already exists
sql -CONVERT函数
Building Survey [1]