java项目中,场景:订单没有付款到期取消订单,使用的是redis过期监听来做的,做个笔记!首先使用该功能需要下载2.8.0及以上的版本,这一部分详细内容可以访问redis官网:http://redis.io/topics/notifications ,以下是我总结的:

第一步:配置redis的过期失效监听,需要修改redis.conf配置文件,找到 EVENT NOTIFICATION (事件通知)这个配置
notify-keyspace-events "" 修改为 notify-keyspace-events "Ex"
在这里插入图片描述

参数解释,看配置文件或者官网都有详细解释,如下:

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

# 键空间通知使得客户端可以通过订阅频道或模式,来接收那些以某种方式改动了 Redis 数据集的事件。
# 因为开启键空间通知功能需要消耗一些 CPU ,所以在默认配置下,该功能处于关闭状态。
# notify-keyspace-events 的参数可以是以下字符的任意组合,
# 它指定了服务器该发送哪些类型的通知:
# K 键空间通知,所有通知以 __keyspace@__ 为前缀
# E 键事件通知,所有通知以 __keyevent@__ 为前缀
# g DEL 、 EXPIRE 、 RENAME 等类型无关的通用命令的通知
# $ 字符串命令的通知
# l 列表命令的通知
# s 集合命令的通知
# h 哈希命令的通知
# z 有序集合命令的通知
# x 过期事件:每当有过期键被删除时发送
# e 驱逐(evict)事件:每当有键因为 maxmemory 政策而被删除时发送
# A 参数 g$lshzxe 的别名
# 输入的参数中至少要有一个 K 或者 E,否则的话,不管其余的参数是什么,都不会有任何 通知被分发。

第二步: 重新启动redis,没什么好说的,修改配置文件以及启动redis不会自行度娘

第三步:验证配置是否成功

  1. 运行redis客户端:
    redis-cli
  2. 选择你要监听的库 @后面的值,redis默认连接0号库,这里不做修改了,运行监听命令:
    psubscribe __keyevent@0__:expired
  3. 在启动一个客户端redis-cli,这里也是默认0号库,不做修改,设置一个10秒过期的key:
    SETEX mykey 10 redis
    拓展:Redis Setex 命令为指定的 key 设置值及其过期时间。如果 key 已经存在, SETEX 命令将会替换旧的值。
  4. 10秒后,可以看到监听端口可以接收到失效的redis的key。如图
    在这里插入图片描述

springboot 项目集成ridis配置过期监听:

1.引入redis依赖,这块不做过多解释,不会的去搜集成reids


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

2.创建两个类,第一个类配置 RedisListenerConfig 实现监听 Redis key 过期时间,我这里是监听所有db的过期事件__keyevent@*__:expired,根据自己的业务需求自行配置,@几号库,@*是所有
RedisListenerConfig:

import org.springframework.beans.factory.annotation.Autowired;
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.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * @ClassName RedisListenerConfig
 * @Description redis监听配置
 * @Author cl
 * @Date 2021-09-09 10:45
 */
@Configuration
public class RedisListenerConfig {
    @Autowired
    RedisListen redisListen;

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory factory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(factory);
        container.addMessageListener(redisListen, new PatternTopic("__keyevent@*__:expired"));
        return container;
    }
}

第二个类,定义监听器 RedisListen,监听 Redis key 过期事件,针对 Redis 数据失效事件,进行业务处理
RedisListen:

import com.tanghe.common.constant.RedisConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

/**
 * @ClassName RedisListen
 * @Description RedisListen
 * @Author cl
 * @Date 2021-09-09 10:46
 */
@Slf4j
@Component
public class RedisListen implements MessageListener {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String expiredKey = message.toString();

        log.info("失效的redis是:"+expiredKey);
        RedisSerializer<?> serializer = redisTemplate.getValueSerializer();
        String channel = String.valueOf(serializer.deserialize(message.getChannel()));
        String body = String.valueOf(serializer.deserialize(message.getBody()));
        log.info("channel==="+channel+"-----------------"+"body === "+body);
        //key过期监听,在处理业务之前校验下自己业务的key和监听的key以及库号
        if("__keyevent@0__:expired".equals(channel) && body.indexOf(“自己业务的key”) != -1){
            log.info("进来了哈");
            //这里写需要处理的业务

        }

    }
}

如您在阅读中发现不足,欢迎留言!!!

附redis常用指令,会持续更新!!!

windows

redis设置开机自启动:redis-server --service-install redis.windows-service.conf --loglevel verbose
redis启动命令:redis-server redis.windows.conf
redis卸载命令:redis-server --service-uninstall
redis启动服务命令:redis-server --service-start
redis停止服务命令:redis-server --service-stop
进入redis命令:redis-cli.exe -h 127.0.0.1 -p 6379
进入redis使用密码命令:redis-cli.exe -h 127.0.0.1 -p 6379 -a password

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐