springboot整合redis缓存过期回调函数实现

  1. 在 redis安装包下 redis.conf 配置文件里面找到 # notify-keyspace-events Ex 去掉注释,然后重启redis.
  2. 添加配置类(这里是简单的配置,详细配置请自己百度一下)
package com.example.test;

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.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

@Configuration
public class RedisConfiguration {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Bean
    public ChannelTopic expiredTopic() {
        return new ChannelTopic("__keyevent@0__:expired");  // 选择0号数据库
    }

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer() {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
        return redisMessageListenerContainer;
    }
}
package com.example.test;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

@Component
public class KeyExpiredListener extends KeyExpirationEventMessageListener {

    public KeyExpiredListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }
	//这里是回调函数失效的时候回调用这个函数
    @Override
    public void onMessage(Message message, byte[] pattern) {
        System.out.println(new String(message.getBody()));
        System.out.println(new String(message.getChannel()));
        System.out.println(new String(pattern));
        super.onMessage(message, pattern);
    }
}
  1. 测试类
package com.example.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class Test implements ApplicationRunner {
    @Autowired
    private StringRedisTemplate redisTemplate;
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public void setRedis() throws ParseException {
        //缓存中最常用的方法
        redisTemplate.opsForValue().set("first", "我是1");
        //关于TimeUnit下面有部分源码截图
        redisTemplate.opsForValue().set("second", "我是2");
        //2019-04-23 16:35:00 缓存失效
        Date date = new Date(dateFormat.parse("2019-04-23 16:35:00").getTime());
        redisTemplate.expireAt("second", date);
        //5秒后缓存失效
        // redisTemplate.opsForValue().set("first", "我是1",5, TimeUnit.SECONDS);
        System.out.println("存入缓存成功");
        System.out.println(redisTemplate.getExpire("second"));
    }
    public void getRedis() {
        String first = redisTemplate.opsForValue().get("first");
        String second = redisTemplate.opsForValue().get("second");
        System.out.println("取出缓存中first的数据是:" + first);
        System.out.println("取出缓存中second的数据是:" + second);
    }
    public void delRedis() {
        //根据key删除缓存
        Boolean first1 = redisTemplate.delete("first");
        System.out.println("是否删除成功:" + first1);
    }
    @Override
    public void run(ApplicationArguments args) throws Exception {
        setRedis();
        getRedis();
    }
}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值