Java listens for redis expiration events


1. Package referencing redis


2. Configure redis listening container

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class RedisListenerConfig {
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        return container;

3. Create redis expiration monitoring


4. Is the test feasible

At this time, you can make an API test and set the expiration event to 30 seconds to see if the key will be monitored normally when it expires.

5. Precautions

Note: there is a requirement that the listener can listen to expired keys in redis. It must be set in the redis configuration file to listen to key expiration events. The configuration file can be found in the directory. The configuration is as follows:

Java listens for redis expiration events

This work adoptsCC agreement, reprint must indicate the author and the link to this article