Springboot 监听redis过期key,获取key进行业务处理

添加相关启动器:

        
            org.springframework.boot
            spring-boot-starter-web
        

application.yml配置

spring:
  redis:
    host: 127.0.0.1
    database: 15
    port: 6379

Redis相关配置:

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate template = new RedisTemplate<>();
        RedisSerializer redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setConnectionFactory(factory);
        //key序列化方式
        template.setKeySerializer(redisSerializer);
        //value序列化
        template.setValueSerializer(jackson2JsonRedisSerializer);
        //value hashmap序列化
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        return template;
    }

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        //解决查询缓存转换异常的问题
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        // 配置序列化(解决乱码的问题),过期时间600秒
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(600))  //设置数据过期时间600秒
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
                .disableCachingNullValues();
        RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
                .cacheDefaults(config)
                .build();
        return cacheManager;
    }

配置二:

@Configuration
public class RedisConfiguration {
    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer() {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
        return redisMessageListenerContainer;
    }
    @Bean
    public RedisKeyExpiredListener keyExpiredListener() {
        return new RedisKeyExpiredListener(this.redisMessageListenerContainer());
    }

}

监听相关配置:

@Log4j2
public class RedisKeyExpiredListener extends KeyExpirationEventMessageListener {
    @Autowired
    private RedisTemplate redisTemplate;
    // 自己的业务服务
    @Autowired
    private IRedisTestService redisTestService;
    public RedisKeyExpiredListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String channel = new String(message.getChannel(), StandardCharsets.UTF_8);
        //过期的key
        String key = new String(message.getBody(),StandardCharsets.UTF_8);
        log.info("redis key 过期:pattern={},channel={},key={}",new String(pattern),channel,key);
        //对key进行业务处理
        System.out.println(redisTestService.chuliKey(key));
    }

}

控制层测试:

@RestController
public class TestRedisController {

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/setObject")
    public String setObject(){
        redisTemplate.opsForValue().set("nihao1", UUID.randomUUID(),20, TimeUnit.SECONDS);
        redisTemplate.opsForValue().set("nihao2",UUID.randomUUID(),20, TimeUnit.SECONDS);
        redisTemplate.opsForValue().set("nihao3",UUID.randomUUID(),20, TimeUnit.SECONDS);
        redisTemplate.opsForValue().set("nihao4",UUID.randomUUID(),20, TimeUnit.SECONDS);
        redisTemplate.opsForValue().set("nihao5",UUID.randomUUID(),20, TimeUnit.SECONDS);
        redisTemplate.opsForValue().set("nihao6",UUID.randomUUID(),20, TimeUnit.SECONDS);
        return "ok";
    }

}

运行结果:

Springboot 监听redis过期key,获取key进行业务处理_第1张图片

 

你可能感兴趣的:(数据库操作,JAVA小工具,redis,spring,boot,java)