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

Spring boot + redis converts expired events into application events

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of "spring boot + redis to convert expired events into application events". 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!

Modify redis.conf

# K key space notification, prefixed with _ _ keyspace@__ # E key event notification Notification of general commands that are prefixed with _ _ keysevent@__ # g del, expipre, rename, etc. # $String command # l List command # s Set command # h Hash command # z ordered collection command # x expiration event (generated each time key expires) # e alias for eviction event (generated when key is full when memory is cleared) # A g$lshzxe, so "AKE" means all events

Notify-keyspace-events Ex

two。 Add RedisMessageListenerContainer configuration

@ Beanpublic RedisMessageListenerContainer container (RedisConnectionFactory connectionFactory) {RedisMessageListenerContainer container = new RedisMessageListenerContainer (); container.setConnectionFactory (connectionFactory); return container;}

3. Add KeyExpirationEventMessageListener configuration

@ Beanpublic KeyExpirationEventMessageListener keyExpirationEventMessageListener (RedisMessageListenerContainer container) {return new KeyExpirationEventMessageListener (container);}

KeyExpirationEventMessageListener will automatically subscribe to the Topic of _ _ keyevent@*__:expired.

4. Add an event listener

@ Component@Slf4jpublic class RedisKeyExpireListener implements ApplicationListener {@ Override public void onApplicationEvent (RedisKeyExpiredEvent event) {log.debug ("{}: {}", event.getId (), event.getKeyspace (), event.getValue (), event.getChannel ());}

It is very convenient to use spring to listen for expired events. You only need a few simple Bean configurations. Spring will convert redis expired events into spring application events. We only need to have a listener to listen for RedisKeyExpiredEvent events.

This is the end of the content of "spring boot + redis to convert expired events into application events". 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

Internet Technology

Wechat

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

12
Report