SpringBoot项目监听reids中过期的key

使用redission客户端操作redis
 

maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.2.8.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson</artifactId>
    <version>3.9.0</version>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson-spring-boot-starter</artifactId>
    <version>3.9.0</version>
</dependency>

redis配置文件

#Redisson配置
singleServerConfig:
  address: "redis://127.0.0.1:6379"
  password: 123456
  clientName: null
  database: 0 #选择使用哪个数据库0~15
  idleConnectionTimeout: 10000
  pingTimeout: 30000
  connectTimeout: 10000
  timeout: 30000
  retryAttempts: 30
  retryInterval: 1500
  reconnectionTimeout: 3000
  failedAttempts: 30
  subscriptionsPerConnection: 5
  subscriptionConnectionMinimumIdleSize: 1
  subscriptionConnectionPoolSize: 50
  connectionMinimumIdleSize: 200
  connectionPoolSize: 2000
  dnsMonitoringInterval: 5000
  #dnsMonitoring: false

threads: 2
nettyThreads: 2
codec:
  class: "org.redisson.client.codec.StringCodec"
transportMode: "NIO"

配置类文件主要有三个
 

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.io.IOException;

@Configuration
public class RedissonConfig {
    @Bean
    @Primary
    public RedissonClient redisson() throws IOException {
        // 本例子使用的是yaml格式的配置文件,读取使用Config.fromYAML,如果是Json文件,则使用Config.fromJSON
        Config config = Config.fromYAML(RedissonConfig.class.getClassLoader().getResource("redisson-config.yml"));
        return Redisson.create(config);
    }
}
import org.redisson.api.RedissonClient;
import org.redisson.spring.data.connection.RedissonConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

@Configuration
public class RedisListenerConfig {

    @Autowired
    private RedissonClient redisson;

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(MessageListener listener) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(new RedissonConnectionFactory(redisson));
        // 添加 KeyExpirationListener 到监听器容器
        container.addMessageListener(listener, new PatternTopic("__keyevent@*:expired"));
        return container;
    }
}
import cn.hutool.core.collection.CollectionUtil;
import com.hs.customer.constant.RedisCacheKey;
import com.hs.customer.dao.TUserDAO;
import com.hs.customer.enums.RoleCodeType;
import com.hs.customer.pojo.dto.UserAllDTO;
import com.hs.customer.service.impl.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
@Slf4j
public class KeyExpirationListener implements MessageListener {

 

    @Override
    public void onMessage(Message message, byte[] bytes) {
        String expiredKey = new String(message.getBody());
        log.info("onMessage --> redis 过期的key是:{}", expiredKey);
        try {
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

*****需要修改redis的配置文件 notify-keyspace-events "Ex"

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值