使用Node.js监听Redis上的按键事件
我需要知道数据何时写入 Redis 存储上的特定键。
有没有办法使用 Node.js 监听 Redis 关键事件,还是必须使用 pub/sub 实用程序?
在第二种情况下,最好的方法是什么?
I need to know when data is written on specific keys on a Redis store.
Is there any way to listen to Redis key events using Node.js or do I have to use the pub/sub utilities?
In the second case, what would be the best way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,redis 本身并不原生支持密钥更改通知,尽管有其他替代方案,包括基于发布/订阅的解决方案。
As far as I know redis itself doesn't natively support key change notifications yet although there are other alternatives including a pub/sub based solution.
如果您不想使用 pubsub,则有很多机会使用 Redis 的阻塞操作(BLPOP、BRPOP、BRPOPLPUSH)来实现创造性解决方案。您可以在服务器中专用一个 Redis 连接来阻止,直到将新项目添加到列表中,并在存储密钥后立即添加到该列表(在多重执行中!)。因此,通用的东西可能看起来像这样(您可以使用两个不同的终端来模拟两个不同的连接):
连接 1(连接将在此之后挂起):
连接 2:
执行后返回连接 1:
If you don't want to use pubsub, there's lots of opportunities for creative solutions with Redis' blocking operations (BLPOP, BRPOP, BRPOPLPUSH). You can dedicate a Redis connection in your server to block until a new item is added to a list, and actually add to said list right after you store a key (in a multi exec!). So, something generic might look like this (you can use two different terminals to simulate two different connections):
Connection 1 (connetion will just hang out after this):
Connection 2:
Back to connection 1 after you EXEC: