监听redis键过期事件

redis配置文件修改

 打开redis.window.conf配置文件,找到以下配置段.

############################# 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@<db>__ prefix.
#  E     Keyevent events, published with __keyevent@<db>__ 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
#  of zero or multiple characters. The empty string means that notifications
#  are disabled.
#
#  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

默认配置是  notify-keyspace-events "" ,将其修改为  notify-keyspace-events Ex.

 E和x的解释可在注释信息中找到.

# E  Keyevent事件,以__keyevent @ <db> __前缀发布

# x  过期事件(每次密钥过期时生成的事件)

修改好后,启动redis时,使用redis.windows.conf配置文件启动即可. 


导入依赖jar包

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

Java端针对redis监听的配置

@Configuration
public class RedisListenerConfig {
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }
}

编写监听器,监听redis过期并进行处理

@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
    @Autowired
    private RedisService redisService;
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate;
    /**
     * Creates new {@link Message Listener} for {@code __keyevent@*__:expired} messages.
     *
     * @param listenerContainer must not be {@literal null}.
     */
    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        // message.toString()可以获取失效的key
        String expiredKey = message.toString();
        System.out.println("expiredKey:"+expiredKey);

        if(expiredKey.contains("hadToPay")){
            String examNo = expiredKey.split("_")[1];
            String examItemNo = expiredKey.split("_")[2];
            String scheduledId = expiredKey.split("_")[3];
            String serviceId = "cn-gdxiash-examcustom";
            List<ServiceInstance> serviceInstances
                    = discoveryClient.getInstances(serviceId);
            if(serviceInstances==null || serviceInstances.isEmpty()){
                //如若未找到服务则当前键延时2分钟
                redisService.set(expiredKey,"scheduledId_"+scheduledId,120);
                return;
            }
            //使用服务的第一个实例测试
            ServiceInstance serviceInstance = serviceInstances.get(0);
            String restUrl = "http://"+ serviceInstance.getHost() +
                    ":"+ serviceInstance.getPort()
                    +"/exam/pay/"+examNo+"/"+examItemNo+"/"+scheduledId;
            //交给examcustom服务处理即可
            System.out.println("The restUrl value is "+restUrl);
            String result = restTemplate.getForObject(restUrl,String.class);
            System.out.println("申请单【"+examNo+"】缴费查询结果:"+result);
        }
    }
}

 实际监听效果展示

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Redis 中,我们可以使用 KEYS 命令来获取所有的 key,但是这个命令性能比较差,因为它会遍历整个 Redis 数据库。为了避免这个问题,我们可以使用 Redis 的发布订阅机制来监听 key 失效事件。 具体步骤如下: 1. 首先我们需要在代码中订阅一个特定的频道,用于接收 key 失效事件的通知。可以使用 Redis 的 SUBSCRIBE 命令来完成订阅操作: ``` SUBSCRIBE __keyevent@0__:expired ``` 2. 接着,我们需要在 Redis 中设置一个 key,并为其设置过期时间。在 key 过期后,Redis 会自动发布一条消息到指定的频道中,我们可以在代码中接收这个消息: ``` SET mykey "hello world" EXPIRE mykey 60 ``` 3. 最后,我们需要编写一个消息处理函数,用于接收并处理 Redis 发布到频道中的消息。在 Python 中,可以使用 Redis 的 Python 客户端库 redis-py 来实现这个功能: ```python import redis r = redis.Redis(host='localhost', port=6379, db=0) def handle_message(message): print(message) pubsub = r.pubsub(ignore_subscribe_messages=True) pubsub.subscribe('__keyevent@0__:expired') for message in pubsub.listen(): handle_message(message['data']) ``` 在上面的代码中,我们首先创建了一个 Redis 客户端对象,并定义了一个消息处理函数 handle_message。然后,我们创建了一个 pubsub 对象,并使用 ignore_subscribe_messages 参数来忽略订阅时产生的消息。接着,我们使用 subscribe 方法来订阅指定的频道,并使用 listen 方法来循环接收 Redis 发布到频道中的消息,并将其传递给 handle_message 函数进行处理。 当我们运行上面的代码并设置了一个 key 的过期时间后,如果这个 key 过期了,Redis 就会自动发布一条消息到指定的频道中,我们就可以在代码中接收到这个消息,并进行相应的处理了。 ### 回答2: 监听Redis缓存过期事件(Key失效事件)可以通过Redis的订阅发布功能来实现。在Redis中,可以通过配置空间通知(keyspace notifications)来启用这个功能。 首先,我们需要在Redis配置文件中打开空间通知功能。可以通过修改redis.conf文件中的`notify-keyspace-events`参数来开启空间通知。例如,可以将其设置为`Ex`,表示启用过期事件的通知。 启用空间通知后,我们可以使用`SUBSCRIBE`命令来订阅指定的频道,以便监听特定事件。对于过期事件,我们可以订阅`__keyevent@0__:expired`频道。 以下是监听Redis缓存过期事件的示例代码: ``` import redis def event_handler(message): print("Key expired:", message['data']) def main(): r = redis.Redis(host='localhost', port=6379, db=0) pubsub = r.pubsub() pubsub.subscribe('__keyevent@0__:expired') for message in pubsub.listen(): if message['type'] == 'message': event_handler(message) if __name__ == "__main__": main() ``` 在上述示例代码中,我们使用Python的`redis`模块来连接Redis,并创建一个订阅对象`pubsub`。然后,我们使用`subscribe`方法来订阅`__keyevent@0__:expired`频道,并通过循环监听事件。 当有过期事件发生时,`event_handler`函数会被调用,并打印出相应的信息。 通过以上代码,我们可以在Redis中实现监听缓存过期事件的功能。当监听过期事件时,可以做相应的处理操作,例如重新加载缓存数据或执行其他业务逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值