Redis事件通知示例

更新时间:2023-06-25 18:49:41 阅读: 评论:0

Redis事件通知⽰例1. redis如同zk⼀样,提供了事件监听(或者说是回调机制),下⾯是redis的配置说明:
>>>>>#### EVENT NOTIFICATION >>>>>>
# Redis can notify Pub/Sub clients about events happening in the key space.
# This feature is documented at 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 Databa 0, two
# messages will be published via Pub/Sub:
#
# PUBLISH __keyspace@0__:foo del
# PUBLISH __keyevent@0__:del foo
#
# It is possible to lect the events that Redis will notify among a t
# of class. 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 t 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 compod
#  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, u:
#
#  notify-keyspace-events Elg
#
#  Example 2: to get the stream of the expired keys subscribing to channel
#            name __keyevent@0__:expired u:
#
#  notify-keyspace-events Ex
#
#  By default all notifications are disabled becau most urs 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过期时,客户端收到通知
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="maven.apache/POM/4.0.0" xmlns:xsi="www.w3/2001/XMLSchema-instance"        xsi:schemaLocation="maven.apache/POM/4.0.0 maven.apache/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 默认连接⼯⼚:" + Class());
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.tConnectionFactory(connectionFactory);
return container;
}
}
import org.tion.Message;
import org.dis.listener.KeyExpirationEventMessageListener;
import org.dis.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:"+String());
}
}
启动springboot即可
验证成功!

本文发布于:2023-06-25 18:49:41,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1054663.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:说明   事件   提供
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图