Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Spring boot + redis converts expired events into application events

Shulou Source: shulou.com Published: 2022-06-02 08:30:19 10月14日 Update

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!

Tags: Events commands monitoring applications configuration content prefixes more knowledge generation practical orderly successful learning next memory aliases dilemmas actual situation meaning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Xiaomi MariaDB macOS OPPO Reno