最佳答案
如果你的 redis 服务器 127.0.0.1:1002 是 slaveof 127.0.0.1:1001,那么你可以像那样执行你的“写入”命令
redisClient.StringSet("key", "value", flags: CommandFlags.DemandMaster);
另外,我注意到,redisClient 本质上会执行需要在 master 上“写入”的命令。对于“读取”命令,您可以编写
redisClient.StringGet("key", flags: CommandFlags.DemandSlave);
但是如果你的奴隶不可用,你会得到一个异常(exception)。另一种选择
CommandFlags.PreferSlave
来自文档:
This operation should be performed on the slave if it is available, but will be performed on a master if no slaves are available. Suitable for read operations only.
关于客户端。如何分离读/写操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44143463/