监听 Redis 的变化? [英] Listen for changes in Redis?
问题描述
我希望能够在我的 redis 集合中进行任何更改时运行回调.回调会将键和值作为输入.这样的事情可能吗?
I want to be able to run a callback when any change is made in my redis collection. The callback would take the key and value as inputs. Is something like this possible?
谢谢?
推荐答案
您还可以使用同步命令像关注者一样连接到 Redis 服务器.请参阅 Redis 复制的工作原理? 以获取快速介绍.
You can also connect to the Redis server like a follower using the sync command. See How Redis Replication Works? for a quick introduction.
同步命令的输出有两个阶段.在第一阶段,服务器返回数据库 dump.rdb 文件.文件发送完后,开始发送Redis协议中的命令,也是AOF格式.
The output of sync command has two phases. In the first phase, the server returns the database dump.rdb file. Once the file is sent, it starts sending commands in the Redis protocol, which is also the AOF format.
以下是您可以做什么的高级图片:
Here is the high level picture of what you can do :
- 连接到 Redis 服务器并发出
SYNC
命令 - 保存并解析 dump.rdb 文件.构建初始数据集.基于node.js的rdb解析器可用
- 解析后面的命令.由于它们采用 Redis 协议,因此您可以从现有的 Redis 库开始.
- 对于您收到的每个命令,调用一个回调
看起来工作量很大,但你应该能够很容易地破解它.它也会成为一个很好的开源库!
It seems a lot of work, but you should be able to hack this pretty easily. And it would make a good open source library too!
同步监视器
Monitor
是一个调试命令.响应格式可以(并且已经)随着时间的推移而改变(d).Sync
用于 Master ->从属复制,因此将得到更好的支持Monitor
将发出所有命令,包括只读命令.Sync
只会为您提供修改数据的命令.Monitor
将记录在 lua 脚本中执行的单个命令.Sync
只会传输整个 lua 脚本,因此您必须自己解析脚本.这确实是sync
的交易破坏者.Monitor
将记录未成功的命令,Sync
将仅记录修改数据的命令.例如,命令del non-existing-key
将被监视器记录,但在您运行同步时不会显示.
Monitor
is a debugging command. The response format can (and has) change(d) over time.Sync
is used for Master -> Slave replication, and so will be better supportedMonitor
will emit all commands, including read-only commands.Sync
will only get you commands that modify data.Monitor
will log individual commands that are executed within a lua script.Sync
will only transfer the entire lua script, so you will have to parse the script yourself. This is indeed a deal breaker forsync
.Monitor
will log commands that did not succeed,Sync
will only log commands that modify data. For example, the commanddel non-existing-key
will be logged by monitor but won't show up when you run sync.
这篇关于监听 Redis 的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!