zoukankan      html  css  js  c++  java
  • Redis---监听Key过期事件

    在实际的开发项目中,监听 key 的过期事件,应用非常广泛,例如:优惠券过期,处理各种超时事件等等

    先贴出SpringBoot工程结构

    实现步骤: 

    1.修改Redis配置文件

       找到 redis.windows.conf或redis.conf 文件,搜索 “notify-keyspace-events”找到原本的notify-keyspace-events " ",修改为 “notify-keyspace-events Ex”,这样我们的 Redis 就支持 key 过期事件的监听了

      

     2.添加依赖

    <!-- redis -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    3.配置yml

    spring:
      application:
        name: Redis
      redis:
        host: 127.0.0.1
        port: 6379

    4.创建监听类

    package com.zk.redis.redisTimeoutEvent;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.connection.Message;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
    import org.springframework.data.redis.listener.RedisMessageListenerContainer;
    import org.springframework.stereotype.Component;
    import java.nio.charset.StandardCharsets;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.concurrent.TimeUnit;
    
    @Slf4j
    @Component
    public class KeyExpiredListener extends KeyExpirationEventMessageListener {
    
        @Autowired
        public RedisTemplate<String,String> redisTemplate;
    
        public KeyExpiredListener(RedisMessageListenerContainer listenerContainer) {
            super(listenerContainer);
        }
    
        @Override
        public void onMessage(Message message, byte[] bytes) {
    //获取失效key名称 String expireKey = new String(message.getBody(), StandardCharsets.UTF_8);
    //获取key原本的value 获取不到 是null String expireKeyValue
    = redisTemplate.opsForValue().get("myKey"); log.info("expireKey---"+expireKey); log.info("expireKeyValue---"+expireKeyValue); } }

    5.创建一个配置类RedisListenerConfig

    package com.zk.redis.redisTimeoutEvent;
    
    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;
        }
    }

    6.写一个Controller测试一下

    package com.zk.redis.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    
    @RestController
    public class RedisController {
    
        @Autowired
        public RedisTemplate<String,String> redisTemplate;
    
        @RequestMapping(value = "/redisTest", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
        public Map<String,Object> redisTest(){
    //redis中存入5秒失效的key redisTemplate.opsForValue().set("myKey", "myValue",5, TimeUnit.SECONDS); String myKey = redisTemplate.opsForValue().get("myKey"); Map<String,Object> resultMap = new HashMap<String,Object>(); resultMap.put("myKey",myKey); return resultMap; } }

    7.项目运行后,浏览器输入访问地址,输出结果,说明key失效后触发onMessage().

    上图说明成功监听并触发事件.

    我话讲完!谁赞成?谁反对?
  • 相关阅读:
    flink(七) 电商用户行为分析(七)订单支付实时监控之订单超时、订单交易匹配
    flink(六) 电商用户行为分析(六)恶意登录监控之连续登陆超时
    flink(五) 电商用户行为分析(五)市场营销商业指标统计分析之市场推广统计、广告点击量统计、 黑名单过滤
    flink(四) 电商用户行为分析(四)实时流量统计(二)网站独立访客数(UV)
    flink(三) 电商用户行为分析(三)实时流量统计(一)热门页面浏览量、网站总浏览量
    flink(二) 电商用户行为分析(二)实时热门商品统计(计算最热门 Top N 商品)
    flink(一) 电商用户行为分析(一)项目整体介绍
    Cause: No supported Ethernet device found + Unknown symbol in module
    vfio_enable_intx
    dpdk disable 收发 interrupt + l3fwd-power
  • 原文地址:https://www.cnblogs.com/wffzk/p/15267104.html
Copyright © 2011-2022 走看看