技术博客 技术博客
  • JAVA
  • 仓颉
  • 设计模式
  • 人工智能
  • Spring
  • Mybatis
  • Maven
  • Git
  • Kafka
  • RabbitMQ
  • RocketMQ
  • Redis
  • Zookeeper
  • Nginx
  • 数据库套件
  • MySQL
  • Elasticsearch
  • MongoDB
  • Hadoop
  • ClickHouse
  • Hbase
  • Hive
  • Flink
  • Flume
  • SQLite
  • linux
  • Docker
  • Jenkins
  • Kubernetes
  • 工具
  • 前端
  • AI
GitHub (opens new window)
  • JAVA
  • 仓颉
  • 设计模式
  • 人工智能
  • Spring
  • Mybatis
  • Maven
  • Git
  • Kafka
  • RabbitMQ
  • RocketMQ
  • Redis
  • Zookeeper
  • Nginx
  • 数据库套件
  • MySQL
  • Elasticsearch
  • MongoDB
  • Hadoop
  • ClickHouse
  • Hbase
  • Hive
  • Flink
  • Flume
  • SQLite
  • linux
  • Docker
  • Jenkins
  • Kubernetes
  • 工具
  • 前端
  • AI
GitHub (opens new window)
  • kafka

    • kafka-2.7.0 基本概念
    • Kafka-2.7.0 搭建及参数解析
    • kafka-2.7.0 spring boot 集成 kafka
    • kafka-2.7.0 kafka Connect
    • kafka-2.7.0 Kafka Streams 流处理
  • RabbitMQ

    • rabbitmq 简介
  • RocketMQ

    • RocketMQ 基础概念
    • RocketMQ 搭建
    • RocketMQ 整合spring boot
  • redis

    • Redis 介绍及安装
    • Redis 命令介绍
    • Redis 分布式锁介绍
    • Redis 事务介绍
    • Redis 的key失效通知介绍
      • 修改 redis.conf 配置
      • spring boot redis key 失效通知配置
        • 添加配置类
        • 接口
        • 实现类
    • Redis 配置文件解读
    • Redis 记一次宕机排查
    • Redis 高可用(一) 主从理论
    • Redis 高可用(二) 哨兵理论
    • Redis 高可用(三) 搭建
    • Redis 集群搭建
  • zookeeper

    • Zookeeper 介绍及安装
    • Zookeeper 做为锁使用
  • nginx

    • nginx-1.18.0 安装
    • nginx 常见问题总结
    • nginx 高可用
  • 数据库套件

    • MyCat 1.6.7(一)MySQL高可用及分库分表
    • MyCat 1.6.7(二)高可用及权限
    • shardingsphere 4.x(一)Sharding-JDBC使用
    • shardingsphere 4.x(二)Sharding-Proxy使用
目录

Redis 的key失效通知介绍

# 修改 redis.conf 配置

找到 notify-keyspace-events 并将 notify-keyspace-events 修改为 notify-keyspace-events Ex

配置 Ex 对应的意思如下: E: 键事件通知,以 __keysevent@<db>__ 为前缀 x: 过期事件(每次 key 过期时生成)

  • K 键空间通知,以 __keyspace@<db>__ 为前缀
  • E 键事件通知,以 __keysevent@<db>__ 为前缀
  • g del , expipre , rename 等类型无关的通用命令的通知,...
  • $ String 命令
  • l List 命令
  • s Set 命令
  • h Hash 命令
  • z 有序集合命令
  • x 过期事件(每次 key 过期时生成)
  • e 驱逐事件(当 key 在内存满了被清除时生成)
  • A g$lshzxe 的别名,因此”AKE” 意味着所有的事件
notify-keyspace-events选项
服务器配置的notify-keyspace-events选项决定了服务器所发送通知的类型:
可以设置的类型如下:
想让服务器发送所有类型的键空间通知和键事件通知,可以将选项的值设置为AKE
想让服务器发送所有类型的键空间通知,可以将选项的值设置为AK
想让服务器发送所有类型的键事件通知,可以将选项的值设置为AE
想让服务器只发送和字符串键有关的键空间通知,可以将选项的值设置为K$
想让服务器只发送和列表键有关的键事件通知,可以将选项的值设置为El
备注:notify-keyspace-events选项的默认值为空,所以如果不设置上面的值,SUBSCRIBE命令不会有任何效果
1
2
3
4
5
6
7
8
9

其中原理就是使用了 redis 的发布订阅功能。

# spring boot redis key 失效通知配置

# 添加配置类

package com.giant.cloud.config;

import com.giant.cloud.support.ReceiveNoticeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
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.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author big uncle
 * @date 2020/7/13 16:24
 * @module
 **/
@Configuration
@ConditionalOnBean(ReceiveNoticeMessage.class)
@Slf4j
public class RedisNoticeConfig {

    @Value("${spring.redis.database:0}")
    private Integer db;

    @Autowired
    ReceiveNoticeMessage receiveNoticeMessage;

    /**
     * 将 Receiver注册为一个消息监听器,并指定消息接收的方法(expiredMessage)
     * 如果不指定消息接收的方法,消息监听器会默认的寻找Receiver中的handleMessage这个方法做为消息接收的方式
    **/
    @Bean
    MessageListener messageListener(){
        return new MessageListenerAdapter(receiveNoticeMessage(),"noticeMessage");
    }

    @Bean
    ReceiveNoticeMessage receiveNoticeMessage(){
        return receiveNoticeMessage;
    }

    /**
     * 设置监听类型
    **/
    @Bean
    public List<ChannelTopic> channelTopic(){
        String name = "__keyevent@"+db.intValue()+"__:%s";
        List<String> commands = Arrays.asList("set","lpush","expired");
        return commands.stream().map(i -> new ChannelTopic(String.format(name,i))).collect(Collectors.toList());
    }

    /**
     * 通过 RedisMessageListenerContainer 配置监听生效
    **/
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(@Autowired RedisConnectionFactory redisConnectionFactory){
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
        redisMessageListenerContainer.addMessageListener(messageListener(),channelTopic());
        return redisMessageListenerContainer;
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

# 接口

package com.giant.cloud.support;

/**
 * @author big uncle
 * @date 2020/7/13 17:13
 **/
public interface ReceiveNoticeMessage {
    /**
     * 失效接收方法
     * @author big uncle
     * @date 2020/7/13 17:14
     * @param key
     * @param channel
     * @return void
    **/
    void noticeMessage(String key,String channel);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 实现类

package com.giant.cloud.monitor;

import com.giant.cloud.support.ReceiveNoticeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author big uncle
 * @date 2020/10/9 17:49
 * @module
 **/
@Component
@Slf4j
public class RedisKeyMonitor implements ReceiveNoticeMessage {

    @Override
    public void noticeMessage(String key,String channel) {
        log.debug("key is {}, channel is {}",key,channel);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
上次更新: 6/11/2025, 4:10:30 PM
Redis 事务介绍
Redis 配置文件解读

← Redis 事务介绍 Redis 配置文件解读→

Theme by Vdoing | Copyright © 2023-2025
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式