zoukankan      html  css  js  c++  java
  • 使用Redis Key失效事件实现定时任务

    修改 redis 相关事件配置

    • 找到 redis 配置文件 redis.conf,查看 notify-keyspace-events 配置项,如果没有,添加 notify-keyspace-events Ex,如果有值,则追加 Ex,相关参数说明如下:
    K:keyspace 事件,事件以 keyspace@ 为前缀进行发布
    E:keyevent 事件,事件以 keyevent@ 为前缀进行发布
    g:一般性的,非特定类型的命令,比如del,expire,rename等
    $:字符串特定命令
    l:列表特定命令
    s:集合特定命令
    h:哈希特定命令
    z:有序集合特定命令
    x:过期事件,当某个键过期并删除时会产生该事件
    e:驱逐事件,当某个键因 maxmemore 策略而被删除时,产生该事件
    A:g$lshzxe的别名,因此”AKE”意味着所有事件
    

    引入依赖

    • 在 pom.xml 中添加 org.springframework.boot:spring-boot-starter-data-redis 依赖
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    

    代码编写

    • 定义配置 RedisListenerConfig 实现监听 Redis key 过期时间
    package com.format.common.config;
    
    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;
    
    /**
     * @Description: redis 监听
     * @Author Format
     * @Date 2020/11/10 14:26
     * @Version V1.0
     */
    
    @Configuration
    public class RedisListenerConfig {
    
        @Bean
        RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
            RedisMessageListenerContainer container = new RedisMessageListenerContainer();
            container.setConnectionFactory(connectionFactory);
            return container;
        }
    
    
    }
    
    
    • 定义监听器 RedisKeyExpirationListener,实现KeyExpirationEventMessageListener 接口,查看源码发现,该接口监听所有 db 的过期事件 keyevent@*:expired"
    package com.format.common.handler;
    
    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;
    
    /**
     * @Description: 监听所有db的过期事件__keyevent@*__:expired"
     * @Author Format
     * @Date 2020/11/10 14:32
     * @Version V1.0
     */
    
    @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) {
    
            // 获取到失效的 key,进行取消订单业务处理
            String expiredKey = message.toString();
            System.out.println(expiredKey);
        }
    
    }
    
    
  • 相关阅读:
    Vue学习之webpack中使用vue(十七)
    Vue学习之Babel配置(十六)
    Vue学习之webpack调用第三方loader(十五)
    JAVA基础之设置随机成语验证码
    JAVA基础之HttpServletResponse响应
    Vue学习之npm常用命令及参数小结(十四)
    EXCEL 查找某个字符在字符串中最后一次出现的位置
    SQLSERVER存储过程基本语法
    SQL SERVER 字符串函数 STRING_SPLIT()
    SQL SERVER 字符串函数 PATINDEX()
  • 原文地址:https://www.cnblogs.com/format-ch/p/14862148.html
Copyright © 2011-2022 走看看