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();
    }
}

你可能感兴趣的:(redis)