当前位置:网站首页>Redis key notification related notes (event notification)
Redis key notification related notes (event notification)
2022-07-17 22:55:00 【Charming lemon】
List of articles
One 、 Introduction to event notification
1、 Preface
Official references :https://redis.io/docs/manual/keyspace-notifications/
from Redis 2.8.0 Start ,Redis Joined the Release / A subscription model as well as Key space message reminder (keyspace notification) function . Key space message reminders allow clients to subscribe to specified channels Redis The ability to change data . It should be noted that , Key space message alerts are not reliable , It does not confirm whether the subscriber has received the message . For example, a subscribed client is temporarily disconnected , Events that occur until the connection is restored will not be available again .
2、 Configuration details
Various versions Redis The configuration file :https://redis.io/docs/manual/config/
It can be done by redis Of redis.conf Configuration in file notify-keyspace-events Parameter can specify what type of notification the server sends . The following is a description of some parameters . This feature is off by default .
| character | notice |
|---|---|
| K | Key space notification , All notices to [email protected] The prefix |
| E | Key event notification , All notices to [email protected] The prefix |
| g | DEL 、 EXPIRE 、 RENAME Notification of generic commands that are not related to the type |
| $ | String command notification |
| l | Notification of list commands |
| s | Notice of assembly order |
| h | Notification of hash command |
| z | Notice of an ordered set order |
| x | Overdue Events : Send whenever an expired key is deleted |
| e | deportation (evict) event : Whenever there is a key because maxmemory Send when policy is deleted |
| A | Parameters g$lshzxe Another name for |
redis> CONFIG GET notify-keyspace-events
1) "notify-keyspace-events"
2) ""
redis> CONFIG SET notify-keyspace-events KEA
OK
redis> CONFIG GET notify-keyspace-events
1) "notify-keyspace-events"
2) "AKE"
In the above example notify-keyspace-events Configure to KEA, It represents all events except for the fate . among ,K And E Represents two types of events ——Keyspace And Keyevent.
Keyspace Represents the message related to the event name , For example, subscribe to the operation event of the specified key ;Keyevent Represents the message related to the key name , For example, subscribe to the relevant key name of the key expiration event .
3、 Subscribe to specified events
After the configuration is complete , It can be done by SUBSCRIBE Command subscription specifies the channel to subscribe to one or more specified events . For example, through subscription [email protected]__:expired Realize subscription database 0 Key expiration event in
The format of the subscribed channel is __<type>@<db>__:<event>, It includes event types (keyspace or keyevent)、 database ( Like databases 0) And events ( for example expired) Three parts . in addition , It can also be done through PSUBSCRIBE The command subscribes to one or more channels matched by composite regular expressions . For example, through subscription __key*@*__:* subscribe Redis All events in all databases in .
4、 Command Events
Redis Different events are provided for many commands , In this article, some commands and their corresponding events will be introduced :
DEL: Occurs when a key is deleteddeleventEXPIRE、PEXPIRE、EXPIREATas well asPEXPIREAT: When setting the timestamp of positive expiration time or future time , Thenexpireevent , Otherwisedelevent ( Will be deleted immediately )SETAnd similarSETEX、SETNX、GETSET: producesetevent , If you useSETEXThen there will beexpireeventMSET: One will be generated for each keyseteventLPUSH、LPUSHXAndRPUSH、RPUSHX: According to the direction of insertionlpushorrpusheventRPOP、LPOP: Produce separatelyrpopAndlpopevent , If the last element in the list is removed , Will be produced at the same timedeleventLSET: producelseteventLREM: producelremevent , Similarly, if the removed element is the last element in the list, it will be generated at the same timedeleventHSET、HSETNXas well asHMSET: Produce ahseteventHDEL: Produce ahdelevent , And when the hash table is empty after removaldeleventSADD: Produce asaddeventSREM: Produce asremevent , And when the set is empty after removaldeleventSMOVE: Generated in the original keysremEvent and generated in the target keysaddeventSINTERSTORE、SUNIONSTORE、SDIFFSTORE: Produce separatelysinterstore、sunionstoreas well assdiffstoreevent , And when the result is an empty set and the target key exists , Will producedeleventZADD: No matter how many elements are added, only onezaddeventZREM: No matter how many elements are removed, only onezremevent , When the ordered set is empty after removaldeleventXADD: producexaddevent , If you useMAXLENSubcommands may be generated at the same timextrimeventXDEL: producexdeleventPERSIST: If the expiration event associated with the corresponding key is successfully removed , ThenpersisteventOccurs when the key expires
expiredeventIn achieving
maxmemoryGenerated when key elimination occurs after the set memory valueevictedevent……
For more command related events , Please refer to keyspace notification The related documents
5、 Some examples
Subscription key expiration event
redis1> SUBSCRIBE [email protected]__:expired
1) "subscribe"
2) "[email protected]__:expired"
3) (integer) 1
# redis2> SETEX greeting 1 "hello world"
# wait for 1 Seconds later :
1) "message"
2) "[email protected]__:expired"
3) "greeting"
Subscribe to all events
redis1> PSUBSCRIBE __key*@*__:*
1) "psubscribe"
2) "__key*@*__:*"
3) (integer) 1
# redis2> SET greeting "hello world"
1) "pmessage"
2) "__key*@*__:*"
3) "[email protected]__:greeting"
4) "set"
1) "pmessage"
2) "__key*@*__:*"
3) "[email protected]__:set"
4) "greeting"
Two 、SpringBoot Realization Redis Invalid listening event
1、 The scene that
be based on Redis Active event handling , such as : When a user purchases a membership card and fails to pay within ten minutes , You need to use applet or APP Actively push the advantages of purchasing membership cards to users , Guide users to continue to complete payment and purchase, etc , Similar scenarios require users to After the specified time point Take the initiative to inform or continue to guide , Use Redis Expiration key Event grace 、 Fast implementation .
because Redis The subscription / Release is No, ACK Acknowledgement mechanism Of , Therefore, messages may be lost , If you want to ensure that the information is reliable , You should choose MQ Message queuing implementation ,
2、 Code implementation (Redis Stand-alone version )
2.1 Environmental preparation
Import correspondence redis rely on
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
Set up application.yml Connection information
spring:
redis:
host: 127.0.0.1
port: 6379
database: 0
password: 123456
timeout: 20000
lettuce:
pool:
max-active: 8
max-wait: -1
max-idle: 50
min-idle: 0
Finally, change redis.conf To configure notify-keyspace-events
# Default
notify-keyspace-events ""
# Change to
notify-keyspace-events Ex
2.2 Configured to monitor Bean
@Configuration
public class RedisConfig {
/** * key Expired event subscription requires */
@Bean
@ConditionalOnMissingBean(RedisMessageListenerContainer.class)
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
// Connection pool settings
container.setConnectionFactory(connectionFactory);
return container;
}
}
2.3 Configured to monitor key
@Component
@Slf4j
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private final String SET_NX = "setnx:";
private final Integer database = 0;
public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
}
@Override
protected void doRegister(RedisMessageListenerContainer listenerContainer) {
String topic = "[email protected]"+database+"__:expired";
log.info(" Configure which channel to listen to :"+topic);
PatternTopic patternTopic = new PatternTopic(topic);
// Channels can be many , Multiple transmissions list
listenerContainer.addMessageListener(this,patternTopic);
}
@Override
public void onMessage(Message message, byte[] pattern) {
// Get expired key, You can do your own business
String expiredKey = message.toString();
// Look for what you need key Prefix
if(!expiredKey.startsWith("test")){
return;
}
// utilize redis setIfAbsent command , If it is empty set return true, If not empty return false, similar setnx Lock operation
Boolean aBoolean = stringRedisTemplate.opsForValue().setIfAbsent(SET_NX + expiredKey, String.valueOf(System.currentTimeMillis()),10, TimeUnit.SECONDS);
if (aBoolean){
// Avoid repeated consumption when multiple services are listening , You can also lock and unlock
// Be careful : Only invalid key value , Can't get key Corresponding value value
System.out.println(expiredKey);
// The corresponding business code can be carried out here
}
}
}
3、Redis Cluster event listening
Here is a brief idea , Because the cluster cannot be monitored , Then set up multiple redis Connect , For each redis Of key Monitoring after expiration , It is equivalent to registering multiple listeners , Finally, expire key Just filter
3、 ... and 、 Some of the problems
Reference resources : Leader : Who uses it again Redis Overdue monitoring enables closing orders , Get out of here !
There are several ways to implement delayed tasks :
Use rocketmq、rabbitmq、pulsar Wait for the delayed delivery function of the message queue
Use redisson Provided DelayedQueue
There are some schemes that are widely circulated but have fatal defects , Don't use it to implement deferred tasks
Use redis Expired monitoring for
Use rabbitmq The dead letter line
Use non persistent time wheels
Redis The expiration monitoring mechanism realizes order timeout processing
边栏推荐
- Hisilicon universal platform construction: online debugging 3vscode plug-in
- 745. 前缀和后缀搜索 : 常规 Trie 运用题
- 阿里巴巴达摩院对话团队招聘研究实习生!
- Oracle表空间由什么构成?
- C language basics: binary search
- Yanrong full flash x NVIDIA Infiniband: creating a high-performance storage technology for GPU computing in the AI Era
- Tab bar switching - simulate the mobile phone to switch viewports with the tab
- 通信方式——8080并口
- How does HMS core security detection service help freshmen prevent Telecom fraud?
- Add new nodes to HAC cluster
猜你喜欢

如何通过策略模式简化 if-else?

Scratch draw changeable squares Electronic Society graphical programming scratch grade examination true questions and answers analysis June 2022

Leetcode exercise - Sword finger offer 32 - I. print binary tree from top to bottom

Install Emmet plug-in for notepad++

扒一扒ReentrantLock以及AQS实现原理

查找——二叉排序树(一)

JMeter常见错误怎样解决

Land surface eco hydrological simulation and multi-source remote sensing data assimilation and Noah MP model

互联网公司都怎么实现分页的,拿 MySQL 使劲Limit?

Complex entanglement between pointer and array
随机推荐
VS打开工程时提示:文件加载 使用Unicode (UTF-8)编码加载文件***时,有些字节已用Unicode替换字符替换。
Migrate data files in RMAN copy mode
Explain the exercises in Chapter 4 of C language
How does HMS core security detection service help freshmen prevent Telecom fraud?
745. 前缀和后缀搜索 : 常规 Trie 运用题
Maatnesite/excel import and export in laravel
Alibaba Dharma Institute dialogue team recruits research interns!
scratch绘制多变的正方形 电子学会图形化编程scratch等级考试四级真题和答案解析2022年6月
Understanding and operation of array
我参加的活动啊啊
JMeter常见错误怎样解决
745. Prefix and suffix search: General trie application questions
TFTLCD 薄膜晶体管液晶显示器——探索者为例
Huawei cloud from entry to actual combat | cloud container service
Oracle的用户与权限体系是什么?
开源篇--精准定位 模型重心坐标
idea调试中Step into与Force Step into区别
数据库配置白/黑名单
网络安全工具篇2--信息收集篇2
Oracle的錶空間與數據庫有什麼關系?