1. redis如同zk一样,提供了事件监听(或者说是回调机制), 下面是redis的配置说明:

############################# EVENT NOTIFICATION ##############################

# Redis can notify Pub/Sub clients about events happening in the key space.
# This feature is documented at http://redis.io/topics/notifications
#
# For instance if keyspace events notification is enabled, and a client
# performs a DEL operation on key "foo" stored in the Database 0, two
# messages will be published via Pub/Sub:
#
# PUBLISH __keyspace@0__:foo del
# PUBLISH __keyevent@0__:del foo
#
# It is possible to select the events that Redis will notify among a set
# of classes. Every class is identified by a single character:
#
# K Keyspace events, published with __keyspace@<db>__ prefix.
# E Keyevent events, published with __keyevent@<db>__ prefix.
# g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...
# $ String commands
# l List commands
# s Set commands
# h Hash commands
# z Sorted set commands
# x Expired events (events generated every time a key expires)
# e Evicted events (events generated when a key is evicted for maxmemory)
# A Alias for g$lshzxe, so that the "AKE" string means all the events.
#
# The "notify-keyspace-events" takes as argument a string that is composed
# of zero or multiple characters. The empty string means that notifications
# are disabled.
#
# Example: to enable list and generic events, from the point of view of the
# event name, use:
#
# notify-keyspace-events Elg
#
# Example 2: to get the stream of the expired keys subscribing to channel
# name __keyevent@0__:expired use:
#
# notify-keyspace-events Ex
#
# By default all notifications are disabled because most users don't need
# this feature and the feature has some overhead. Note that if you don't
# specify at least one of K or E, no events will be delivered.
notify-keyspace-events "Ex" // 本示例做个key ttl通知

上面就是redis提供的事件通知类型,咱们可以自由组合,官方也提供了两个事件组合示例。

下面是网上找一段说明,可以参考:

2. 测试

下面使用spingboot集成redis做个测试,如果一切正常,应该是key过期时,客户端 收到通知

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>qinfeng.zheng</groupId>
<artifactId>redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redis</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
@SpringBootConfiguration
public class RedisConfig {
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
System.out.println("springboot2 redis 默认连接工厂:" + connectionFactory.getClass());
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
} }
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 RedisCallBack extends KeyExpirationEventMessageListener { public RedisCallBack(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
} /**
* 针对redis key ttl数据处理(回调)
*
* @param message
* @param pattern
*/
@Override
public void onMessage(Message message, byte[] pattern) {
System.out.println("过期key:"+message.toString());
}
}
spring.redis.host=192.168.79.221
spring.redis.port=6379
spring.redis.password=123456
spring.redis.database=0

启动springboot即可

验证成功!

Redis事件通知示例的更多相关文章

  1. Redis 键空间事件通知

    出处: 使用Redis完成定时任务 场景   使用Java做过项目的人大概都用过定时器.一般来说,项目里订单模块和评论模块,都会涉及到定时任务执行.比如说: 用户下订单后,需要在5分钟内完成支付,否则 ...

  2. 如何扩展分布式日志组件(Exceptionless)的Webhook事件通知类型?

    写在前面 从上一篇博客高并发.低延迟之C#玩转CPU高速缓存(附示例)到现在又有几个月没写博客了,啥也不说,变得越来越懒了,懒惰产生了拖延后遗症. 最近一周升级了微服务项目使用的分布式日志组件Exce ...

  3. SQL Server 事件通知(Event notifications)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 基础知识(Rudimentary Knowledge) 事件通知监控DDL(NotifyQue ...

  4. Python中的 redis keyspace 通知

    介绍 Redis是内存中的数据结构存储,用于缓存.高速数据摄取.处理消息队列.分布式锁定等等. 与其他内存存储相比,使用Redis的优势在于它提供了持久性和数据结构,比如列表.集合.排序集合和散列. ...

  5. Redis事件订阅和持久化存储

    http://blog.csdn.net/yinwenjie/article/details/53518286 Redis从2.X版本开始,就支持一种基于非持久化消息的.使用发布/订阅模式实现的事件通 ...

  6. muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制

    目录 muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制 eventfd的使用 eventfd系统函数 使用示例 EventLoop对eventfd的封装 工作时序 runInLoo ...

  7. PHP 命令行模式实战之cli+mysql 模拟队列批量发送邮件(在Linux环境下PHP 异步执行脚本发送事件通知消息实际案例)

    源码地址:https://github.com/Tinywan/PHP_Experience 测试环境配置: 环境:Windows 7系统 .PHP7.0.Apache服务器 PHP框架:ThinkP ...

  8. Caddy源码阅读(二)启动流程与 Event 事件通知

    Caddy源码阅读(二)启动流程与 Event 事件通知 Preface Caddy 是 Go 语言构建的轻量配置化服务器.https://github.com/caddyserver/caddy C ...

  9. Redis键通知机制

    Redis键通知机制 一.概念 自从redis2.8.0以后出了一个新特性,Keyspace Notifications 称为“键空间通知”. 这个特性大概是,凡是实现了Redis的Pub/Sub的客 ...

随机推荐

  1. windows连接ubuntu服务器方式

    如图,打开cmd, 输入 ssh imkow@www.dorian.vip 参数解析: ssh:secure shell的缩写 imknow  是用户名 www.dorian.vip 是域名,没有域名 ...

  2. codeforces 617 E. XOR and Favorite Number(莫队算法)

    题目链接:http://codeforces.com/problemset/problem/617/E 题目: 给你a1 a2 a3 ··· an 个数,m次询问:在[L, R] 里面又多少中 [l, ...

  3. sql server查询结果复制出来,没有换行(存进去的数据是换行的)

    https://stackoverflow.com/questions/53115490/how-to-correctly-insert-newline-in-nvarchar The problem ...

  4. HDU 4321 Arcane Numbers 2

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4321 ----------------------------------------------- ...

  5. Django 前戏

    1.web应用 web应用程序是一种可以通过web访问的应用程序.程序最大的好处就是用户很容易的访问应用程序,用户只需要有浏览器即可,不需要在安装其他的软件,应用程序有两种模式C/S,B/S C/S模 ...

  6. Linux-磁盘配额

    磁盘配额作用是限制普通用户使用的磁盘空间和创建文件的个数,不至于因为个别人的浪费而影响所有人的使用,需要内核的支持 注意:目前只有 ext2 ext3文件系统支持 需要用户程序quota程序包 先查看 ...

  7. WEB服务端安全---认证会话与访问控制

    一.认证与会话管理 认证:简而言之就是通过一定的凭证认出用户是谁.认证过程中按凭证数量可简单分为 ‘单因素认证’.‘双因素认证’或多因素认证.一般来说,多因素认证强度更高,但是用户体验上会比单因素认证 ...

  8. SEC3 - MySQL常见命令

    1.查看当前所有的数据库 show databases; 2. 打开指定的库名 use 库名称: 3.查看当前库中所有的表 show tables; 4. 查看其他库的所有表 show tables ...

  9. [功能集锦] 002 - mysql查询数据库字典+导出+样式一键整合至excel

    写在前面: 因为工作时候经常遇到半路接手项目的情况,由于年代久远,数据库字典这块经常缺失.故写此篇,以便复用,也希望对大家有点帮助. 随笔内容不高级,如有不妥,不吝指正. 20190730-加了一些简 ...

  10. 在Lua中进行运算符重载

    在C++里面运算符是可以重载的,这一点也是C++比较方便的一个地方.在Lua中其实也是可以模拟出运算符的重载的. 在Lua中table中元素的运算都是和一个叫做元表有关的,在一个table型的变量上都 ...