<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.5.10.RELEASE</version>
</dependency>

  

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 RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
} /**
* 针对redis数据失效事件,进行数据处理
* @param message
* @param pattern
*/
@Override
public void onMessage(Message message, byte[] pattern) {
// 用户做自己的业务处理即可,注意message.toString()可以获取失效的key
String expiredKey = message.toString();
System.out.println(expiredKey);
}
}

  

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.RedisMessageListenerContainer; @Configuration
public class RedisListenerConfig { @Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
}

  启动工程.

redis.windows-service.conf 文件需要修改

然后在此基础上把notify-keyspace-events Ex 这一行的注释打开

set a 1 ex 2(2秒后过期)

页面就会打印过期的key为a

最新文章

  1. Android+jsp +html 文件上传案例 已测试 成功通过
  2. XMPP iOS客户端实现三:登录、注册
  3. CSS3的高级特性
  4. d3 API scale
  5. Java多线程9:ThreadLocal源码剖析
  6. 一款基于HTML5的Web 3D开发工具
  7. 【转载】Velocity模板引擎的介绍和基本的模板语言语法使用
  8. Javascript自动换图片
  9. Delphi2010新发现-类的构造和析构函数功能
  10. DBCC CHECKDB用法 手工修复数据库
  11. 【Spring】BeanFactory解析bean详解
  12. css3学习系列之选择器(一)
  13. java集合系列——Map之HashMap介绍(八)
  14. iOS 播放音频的几种方法
  15. global与nonlocal关键字
  16. 20180706001 - 动态添加 tabPage
  17. 视觉显著性简介 Saliency Detection
  18. linux文件管理 文件操作
  19. screen 命令安装使用
  20. C#精华面试题及答案 三

热门文章

  1. [AGC023D] Go Home 题解
  2. FastAPI 学习之路(六)查询参数,字符串的校验
  3. 【UE4 设计模式】组件模式 Components Pattern
  4. 力扣 - 剑指 Offer 53 - II. 0~n-1中缺失的数字
  5. 第五课第四周实验一:Embedding_plus_Positional_encoding 嵌入向量加入位置编码
  6. NOIP模拟84(多校17)
  7. 浅谈如何爆踩TLEcoders
  8. RocketMQ源码详解 | Broker篇 &#183; 其二:文件系统
  9. 使用NLog把日志写入数据库并按天自动分表
  10. 不破不立,祝贺EDG夺得S11冠军。这一夜,我看到太多Flag成真