1. Package referencing redis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2. Configure redis listening container
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
@Configuration
public class RedisListenerConfig {
//from fhadmin.cn
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
}
3. Create redis expiration monitoring
@Component
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:
This work adoptsCC agreement, reprint must indicate the author and the link to this article