Redis过期回调机制
开启配置
第一种方式:
// 注意K针对的是key,E针对的是Event
CONFIG set notify-keyspace-events Kx (但非持久化,redis重启后会失效)
第二种方式(配置文件redis.conf):
// 监听具体key:某个key过期
notify-keyspace-events "Kx"
// 监听事件:所有key过期
notify-keyspace-events "Ex"
springBoot集成
1、创建Redis过期事件监听器:
// 模板
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
@Component
public class RedisKeyExpirationListener implements MessageListener {
@Override
public void onMessage(Message message, byte[] pattern) {
String expiredKey = message.toString();
// 对key进行过滤
if (expiredKey.startsWith("test")){
// 在这里添加你的业务逻辑,比如清除相关数据或发送通知
System.out.println("键过期:" + expiredKey);
}
}
}
// Demo示例
import com.xz.bd.business.domain.TDeviceWarning;
import com.xz.bd.business.mapper.TDeviceWarningMapper;
import com.xz.bd.common.constant.CacheConstants;
import com.xz.bd.common.core.redis.RedisCache;
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 javax.annotation.Resource;
import java.util.List;
@Component
public class RedisKeyExpirationListener implements MessageListener {
@Autowired
private TDeviceWarningMapper deviceWarningMapper;
@Resource
private RedisCache redisCache;
@Override
public void onMessage(Message message, byte[] pattern) {
String expiredKey = message.toString();
// 在这里添加你的业务逻辑,比如清除相关数据或发送通知
// 从副本里读取数据,因为主key已经过期,无法读取到value
List<TDeviceWarning> cacheObject = redisCache.getCacheObject(CacheConstants.WARNING_KEY_VALUE);
for (TDeviceWarning tDeviceWarning : cacheObject) {
// key已过期,将相应数据插入至数据库
deviceWarningMapper.insert(tDeviceWarning);
// 删除副key
redisCache.deleteObject(CacheConstants.WARNING_KEY_VALUE);
}
}
}
2、配置Redis监听器容器
// 模板
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
@Configuration
public class RedisConfig {
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(
RedisConnectionFactory connectionFactory,
MessageListener messageListener) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
// 设置监听的频道,这里监听了"__keyevent@0__:expired"频道,其中0是Redis数据库的索引(*代表所有数据库),即监听0号数据库所有key过期事件
// 注意参数messageListener的类型可以写RedisKeyExpirationListener,避免Bean可能重复问题
container.addMessageListener(messageListener, new PatternTopic("__keyevent@0__:expired"));
return container;
}
}
// Demo 示例
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(
RedisConnectionFactory connectionFactory,
MessageListener messageListener) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
// 监听某个具体的key,即当key为CacheConstants.WARNING_KEY过期时,会被监听到
container.addMessageListener(messageListener, new PatternTopic("__keyspace@*__:"+ CacheConstants.WARNING_KEY));
return container;
}
验证测试
1、打开终端连接redis,并开启订阅
psubscribe __keyspace@*__:warning_key
2、打开第二个终端,新增一个有过期时间的key
// 注意此处的key要与订阅的key保持一致
setex warning_key 20 24
3、监听结果
参考文章