Redis如何使用golang监听1000到10000个列表的更改?

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

Redis how to listen changes in 1000 to 10,000 lists using golang?

问题

我有1000到10,000个键存储在Redis中,它们的值类型是列表。
当任何一个现有列表中添加了新项时,我需要我的golang程序收到通知。
一旦收到通知,我需要生成一个新的goroutine并执行一个小操作。

我正在使用redigo进行Redis连接池。

在不过载Redis实例的情况下,解决这个问题的最佳方法是什么?

英文:

I have 1000 to 10,000 keys stored in Redis, their value type is list.
When a new item is added to any one of the existing lists, I need my golang program to be notified.
Once notification is received I need to spawn a new goroutine and perform a small operation.

I am using redigo for redis connection pool.

What is the best approach to solve this problem, without overloading the Redis instance?

答案1

得分: 5

你可以启用Redis的键空间通知,并订阅你感兴趣的键/模式相关的事件。

更多详细信息可以在文档中找到:http://redis.io/topics/notifications

英文:

You can enable Redis' keyspace notifications and subscribe to the relevant events on the keys/patterns that interest you.

More details can be found in the documentation: http://redis.io/topics/notifications

答案2

得分: 2

我还没有尝试过这个,但是作为猜测,我会使用Redis的Lua来实现两个新的命令 - MSR_PUSH和MSR_POP,分别执行以下操作:

-- MSR_PUSH
redis.call("PUSH", KEYS[1], ARGV[1])
redis.call("PUBLISH", "notify", KEYS[1])

和:

-- MSR_POP
local v = redis.call("POP", KEYS[1])
if v then
redis.call("PUBLISH", "notify", KEYS[1])
end
return v

所以,这些Lua脚本会像通常一样更新列表,但同时也会将被更新的键名发布到notify pub/sub,这将允许一个监视脚本(如golang)执行某些操作。你也可以将其推送到另一个队列,并进行长轮询。

这个链接提供了有关Lua与Redis的更多信息:https://www.redisgreen.net/blog/intro-to-lua-for-redis-programmers/

英文:

I haven't tried this, but as speculation, I would use Redis's Lua to implement 2 new commands - MSR_PUSH and MSR_POP - which would do the following respectively:

-- MSR_PUSH
redis.call("PUSH", KEYS[1], ARGV[1])
redis.call("PUBLISH", "notify", KEYS[1])

and:

-- MSR_POP
local v = redis.call("POP", KEYS[1])
if v then
  redis.call("PUBLISH", "notify", KEYS[1])
end
return v

So, these Lua scripts update the lists as you normally do, but then also publish the keyname that was updated to the notify pub/sub, which will then allow a watching script (golang) to do something. You could also just push to another queue, and long poll that.

This link has more information on Lua with Redis: https://www.redisgreen.net/blog/intro-to-lua-for-redis-programmers/

huangapple
  • 本文由 发表于 2016年1月24日 11:07:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/34971769.html
匿名

发表评论

匿名网友

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

确定