redis订阅怎么退出_通过订阅redis事件实现“时间截止后删除”功能

本文介绍了如何通过Redis的过期事件功能,结合配置notify-keyspace-events为Ex,实现在键过期时自动触发系统操作,例如空间回收。在程序中添加依赖并创建MessageListener,监听过期事件,当接收到过期消息时执行相应的清理逻辑。
摘要由CSDN通过智能技术生成

思考

情景

有一个这样的场景,某系统里为用户开辟了一个空间,这个空间在有效期里可以随意使用。但是到期后要回收。我们可以通过定时任务对数据库表中存在的空间信息进行检查,如果截止时间到了,就进行对应的操作。也可以把这个定时的工程扔给系统以外。

另一种思路

我们可以尝试另一种方案,例如创建空间的时候,将空间id作为key的一部分存放在redis中,而ex设置为有效时间。在redis将这个超时的key删除的时候,通知我们系统,从而完成清除空间的操作。

实施

具体实现如下:

1,打开事件

1.1,修改配置文件

默认的redis并没有开启这个功能,需要修改配置文件中的notify-keyspace-events配置

############################# Event notification ##############################

# Redis can notify Pub/Sub clients about events happening in the key space.

# This feature is documented at http://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 Database 0, two

# messages will be published via Pub/Sub:

#

# PUBLISH __keyspace@0__:foo del

# PUBLISH __keyevent@0__:del foo

#

# It is possible to select the events that Redis will notify among a set

# of classes. Every class is identified by a single character:

#

# K Keyspace events, published with __keyspace@__ prefix.

# E Keyevent events, published with __keyevent@__ prefix.

# g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...

# $ String commands

# l List commands

# s Set commands

# h Hash commands

# z Sorted set 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 composed

# by zero or multiple characters. The empty string means that notifications

# are disabled at all.

#

# Example: to enable list and generic events, from the point of view of the

# event name, use:

#

# notify-keyspace-events Elg

#

# Example 2: to get the stream of the expired keys subscribing to channel

# name __keyevent@0__:expired use:

#

# notify-keyspace-events Ex

#

# By default all notifications are disabled because most users 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

是的,重点就是:

notify-keyspace-events Ex

Ex代表:

# 以 “__keyevent@__“ 为前缀发布Keyevent事件,为所使用的redis库编号

1,E: Keyevent events, published with __keyevent@__ prefix.

# 过期事件(每次键过期时生成的事件)

2,x: Expired events (events generated every time a key expires)

1.2,运行时通过参数设置

#设置:

redis-cli: CONFIG SET notify-keyspace-events "Ex"

#查询

redis-cli:CONFIG GET notify-keyspace-event

#redis: 指的是进入redis-cli后,并且执行成功的情况

但是这种重启redis服务就没了(恢复为配置文件中的设置)。

2,程序方面

2.1,增加依赖包

pom.xml:

org.springframework.data

spring-data-redis

1.8.22.RELEASE

redis.clients

jedis

2.10.2

# spring用的是4.3.7.RELEASE。需要注意匹配。

代码:

import org.springframework.data.redis.connection.Message;

import org.springframework.data.redis.connection.MessageListener;

public class RedisKeyExpiredMessageDelegate implements MessageListener {

private final String EVENT_CHANNEL = "__keyevent@0__:expired";

@Override

public void onMessage(Message message, byte[] bytes) {

if (EVENT_CHANNEL.equalsIgnoreCase(new String(message.getChannel()))) {

String msg = new String(message.getBody());

System.out.println(msg + "过期");

}

}

}

spring配置文件:

p:host-name="localhost"

p:port="6379"/>

class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">

class="org.springframework.data.redis.listener.RedisMessageListenerContainer">

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值