Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Explain in detail how Redis turns on keyspace notification to realize timeout notification.

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian this time to share with you is a detailed explanation of Redis how to open the key space notification to achieve timeout notification, the article is rich in content, interested friends can come to understand, I hope you can read this article after some harvest.

Redis Settings

Modify the configuration file redis.conf(redis.windows.conf for Windows)

Open the configuration file (location depends on your installation location) and locate the Event notification section. Open or add the comment notify-keyspace-events Ex, where E stands for Keyevent, the notification returns the name of the key, and x stands for timeout event. Comment out the notify-keyspace-events "" configuration if it is not commented on, otherwise it will not take effect. After saving, restart redis. Be sure to restart with the current configuration file, such as src/redis-server redis.conf.

SpringBoot section settings

Add redis dependency

org.springframework.boot spring-boot-starter-data-redis

Add redis configuration to global profile application

spring.redis.host = 39.105.145.179spring.redis.port=6379spring.redis.database=0spring.redis.listen-pattern = __keyevent@0__:expired

Listen-pattern fills in timeout, meaning springboot will listen for timeout keyspace notifications from redis.

Creating a listener

public class TopicMessageListener implements MessageListener { @Override public void onMessage(Message message, byte[] bytes) { byte[] body = message.getBody(); byte[] channel = message.getChannel(); System.out.println(new String(body)); System.out.println(new String(channel)); }}

where message is the notification returned by redis, body is the name of the timeout key, channel is the timeout event

Create listener configuration class

@Configurationpublic class RedisListenerConfiguration { @Bean public RedisMessageListenerContainer getListenerContainer (RedisConnectionFactory connectionFactory){ //Create connection container RedisMessageListenerContainer = new RedisMessageListenerContainer(); //Put into redis connection container.setConnectionFactory (connectionFactory); //Write the type to be monitored, i.e. timeout monitoring Topic = new PatternTopic ("__keyevent@0__:expired"); container.addMessageListener(new TopicMessageListener(), topic); return container; }}

Then when a key expires, redis will send a notification to be received by the TopicMessageListener above, and the corresponding business method can be invoked in this class for business processing.

After reading this article on how to open the key space notification to realize timeout notification, if you think the article content is well written, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report