我们可以在共享的Redis键空间事件通知中指定一个过期的模式吗?

huangapple go评论85阅读模式
英文:

Can we specify a pattern on shared Redis keyspace event notification that expired?

问题

我目前正在使用一个共享的Redis服务器,并且能够通过以下配置获取所有过期的Redis键空间事件。

redis.conf

# 使用键空间通知
notify-keyspace-events Ex

应用程序(使用golang和redis/v8)

pubSub := redis.PubSub(ctx, "__keyevent@0__:expired")
for {
    msg, _ := pubSub.ReceiveMessage(ctx)

    if msg.Payload == "customkeyoncache" {
       // 处理
     }

}

我只是好奇是否可以在实际的键空间事件上设置模式匹配?

__keyevent@0__:expired(可以工作,但会接收到所有事件)
__customkeyoncache_*@0__:expired(无法工作)
__keyevent*customkeyoncache@0__:expired(无法工作)

也许有人知道如何在共享服务器上获取所有过期事件。

谢谢

英文:

I am currently using a shared Redis server and was able to get all redis keyspace events that expired using the following configurations below.

redis.conf

# use key-space notification
notify-keyspace-events Ex

application (golang, redis/v8)

pubSub := redis.PubSub(ctx, "__keyevent@0__:expired")
for (
    msg, _ := pubSub.ReceiveMessage(ctx)

    if msg.Payload == "customkeyoncache" {
       // process
     }

)

I am just curious if we can set a pattern on the actual key-space event like?

__keyevent@0__:expired (working, but receiving all)
__customkeyoncache_*@0__:expired (not working)
__keyevent*customkeyoncache@0__:expired (not working)

Maybe someone knows?, getting all the expired events on a shared server.

Thanks

答案1

得分: 1

在Redis中,无法直接在键空间事件通知中设置模式。Redis中的键空间事件通知系统不支持通配符或模式来指定事件。

你只能在固定的键空间级别上订阅特定的事件(例如,过期的键、设置的键、删除的键等)。

在你当前的配置中,当你订阅keyevent@0:expired频道时,你将接收到Redis数据库中所有过期键的通知(正如你提到的,接收所有过期事件)。如果你需要根据特定模式过滤事件,你需要在接收到事件后在客户端端处理过滤。

例如,在你的Go应用程序中,你可以检查msg.Payload的值(表示过期键的名称)是否与你期望的模式匹配,比如customkeyoncache。如果匹配,你可以继续进行进一步的处理。
以下是带有基本模式过滤的代码片段的修改版本:

pubSub := redis.PubSub(ctx, "__keyevent@0__:expired")
for {
    msg, _ := pubSub.ReceiveMessage(ctx)

    // 检查过期的键是否与期望的模式匹配
    if strings.HasPrefix(msg.Payload, "__keyevent@0__:expired") {
        parts := strings.SplitN(msg.Payload, ":", 2)
        key := parts[1]

        if strings.HasPrefix(key, "customkeyoncache_") {
            // 使用期望的模式处理过期的键
        }
    }
}

通过这个修改,你只会处理名称中包含指定模式(customkeyoncache_)的过期键。

请记住,使用键空间事件通知可能会带来一些开销,特别是如果你有许多键频繁过期。因此,请谨慎使用,并考虑对Redis服务器性能的影响。

英文:

In Redis, it is not possible to directly set a pattern on the keyspace event notification. The key-space event notification system in Redis does not support wildcards or patterns for specifying events.

and you can only subscribe to specific events (e.g., expired keys, set keys, deleted keys, etc.) at a fixed keyspace level.

In your current configuration, when you subscribe to the keyevent@0:expired channel, you will receive notifications for all expired keys in the Redis database (as you mentioned, receiving all expired events). If you need to filter the events based on a specific pattern, you will need to handle the filtering on the client side after receiving the events.

For example, in your Go application, you can check the msg.Payload value (which represents the expired key name) to see if it matches your desired pattern, such as customkeyoncache. If it does match, you can then proceed with further processing.
Here's a modified version of your code snippet with basic pattern filtering:

pubSub := redis.PubSub(ctx, "__keyevent@0__:expired")
for {
    msg, _ := pubSub.ReceiveMessage(ctx)

    // Check if the expired key matches the desired pattern
    if strings.HasPrefix(msg.Payload, "__keyevent@0__:expired") {
        parts := strings.SplitN(msg.Payload, ":", 2)
        key := parts[1]

        if strings.HasPrefix(key, "customkeyoncache_") {
            // Process the expired key with the desired pattern
        }
    }
}

With this modification, you'll only process the expired keys that have the specified pattern (customkeyoncache_) in their names.

Remember that using keyspace event notifications can create some overhead, especially if you have many keys expiring frequently. So, make sure to use them judiciously and consider the impact on your Redis server's performance.

huangapple
  • 本文由 发表于 2023年7月19日 14:59:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76718709.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定