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

How to use redis to realize delay Notification function

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to use redis to achieve delayed notification function". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Redis expired listening scenario

There are similar requirements in the business to wait for a certain amount of time to perform some behavior, such as closing the order after 30 minutes. There are many Demo that use Redis expired snooping on the Internet.

Redis configuration

Open the comments on the notify-keyspace-events Ex line

Project demo project

The structure of the project is as follows

Maven dependence

Kim-redis com.kim 1.0.0 4.0.0 kim-redis-expiration-notice org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-redis org.projectlombok lombok Org.apache.commons commons-pool2

Configuration file

Server: port: 20103spring: redis: # Database Index database: 0 host: 127.0.0.1 port: 6379 password: 123456 lettuce: pool: # maximum number of connections max-active: 8 # maximum blocking wait time (negative number indicates no limit) max-wait:-1 # maximum idle max-idle: 8 # minimum idle min-idle: 0 # connection timeout timeout: 10000

Startup class

/ * Project: kim-redis * @ PackageName: com.kim.redis.expiration.notice * @ FileName: NoticeApplication.java * @ Description: The NoticeApplication is... * @ Author: kimwu * @ Time: 2020-12-19 14:01:56 * / @ SpringBootApplicationpublic class NoticeApplication {public static void main (String [] args) {SpringApplication.run (NoticeApplication.class, args);}}

Configuration class

@ Configurationpublic class RedisTimeoutConfiguration {@ Autowired private RedisConnectionFactory redisConnectionFactory; @ Bean public RedisMessageListenerContainer redisMessageListenerContainer () {RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer (); redisMessageListenerContainer.setConnectionFactory (redisConnectionFactory); return redisMessageListenerContainer;} @ Bean public KeyExpiredListener keyExpiredListener () {return new KeyExpiredListener (this.redisMessageListenerContainer ());}}

Monitoring class

@ Slf4jpublic class KeyExpiredListener extends KeyExpirationEventMessageListener {public KeyExpiredListener (RedisMessageListenerContainer listenerContainer) {super (listenerContainer);} @ Override public void onMessage (Message message, byte [] pattern) {String channel = new String (message.getChannel (), StandardCharsets.UTF_8); / / expired key String key = new String (message.getBody (), StandardCharsets.UTF_8) Log.info ("redis key expiration: pattern= {}, channel= {}, key= {}", new String (pattern), channel, key);}} exception test

When the key expires, the project goes down

① writes to the key of redis

② manually shuts down the service, waiting for the key of redis to expire.

After ③ confirms that the key of redis expires, restart the service. The service will not be notified

When key expires, the redis service goes down

① writes to the key of redis

② shuts down the redis service and waits for the key of redis to expire

③ starts the redis service and finds that the expired key of redis no longer exists and the service has not been notified.

Conclusion

Redis's key expiration is inherently unreliable and does not guarantee reliability like rabbitmq.

When the service itself goes down or redis goes down, there is no guarantee that expired key can be consumed.

Redis's key expiration policy can be used when usage scenarios are less accurate about data integrity. Otherwise, it is not recommended to use redis's key expiration policy.

This is the end of the content of "how to use redis to achieve delayed notification". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report