[英]How to use redis WATCH in Node.js?
我有一个原子操作,我需要使用一个锁来防止其他客户端读取不稳定的值。
根据官方文档,解决方案是将WATCH
与MULTI
一起使用:
现在,已记录了MULTI
的用法,我对如何使用它有了一个大致的了解。
var redis = require( "redis" );
var bluebird = require( "bluebird" );
var client = redis.createClient();
var multi = client.multi();
multi.hsetAsync( "test", "array", "[1, 2]" );
multi.hgetAsync( "test", "array" );
multi.execAsync( ).then( console.log ); // [ 0, "[1, 2]" ]
我了解这是multi的正确实现。 首先, 我需要创建一个客户端,然后创建一个多查询。
我知道multi
和client
共享相同的接口,但是也不清楚在multi
查询中使用hgetAsync
而不是hget
有什么好处(因为有的话),因为我假设multi所做的全部就是将请求添加到同步排队(因此,我不需要Aart vartiant)。
调用multi.execAsync( )
后,查询的执行将自动进行 。
但是我不知道WATCH
应该如何进入这里。 我在文档中找不到对它的任何引用,也没有关于REDIS拥有的乐观锁系统的任何信息。
所以我有以下问题:
MULTI
是否支持WATCH
? multi.hgetAsync( "test", "array" );
是否有意义multi.hgetAsync( "test", "array" );
而不是multi.hget( "test", "array" );
? 没有关于在节点redis中使用WATCH的文档。 但是,我确实在MDN中找到了一组非常有用的提示:
https://developer.mozilla.org/zh-CN/docs/Mozilla/Redis_Tips
总而言之,WATCH的用法如下:
var redis = require("redis"),
client = redis.createClient({ ... });
client.watch("foo", function( err ){
if(err) throw err;
client.get("foo", function(err, result) {
if(err) throw err;
// Process result
// Heavy and time consuming operation here
client.multi()
.set("foo", "some heavy computation")
.exec(function(err, results) {
/**
* If err is null, it means Redis successfully attempted
* the operation.
*/
if(err) throw err;
/**
* If results === null, it means that a concurrent client
* changed the key while we were processing it and thus
* the execution of the MULTI command was not performed.
*
* NOTICE: Failing an execution of MULTI is not considered
* an error. So you will have err === null and results === null
*/
});
});
});
因此,回答我的问题:
watch
是呼吁RedisClient
原型,而不是在Multi
原型。 Multi
原型的对象中的每个方法都会返回对象本身,因此使用方法的Async
版本不会带来任何好处,除了execAsync
之外, execAsync
允许您执行多查询并以Promise而不是Promise处理响应。打回来。 另一个真正重要的事情是, watch
仅适用于KEYS, 而不适用于哈希 。 因此,就我而言,您无法观看哈希test
的字段array
。 您可以观看整个test
集,但不能观看特定的字段。
因此,因为在我的代码中,我实际上想监视哈希中的字段。 这是不可能的。 相反,我必须使用一个密钥命名系统来代替:
var redis = require( "redis" );
var bluebird = require( "bluebird" );
var client = redis.createClient();
var multi = client.multi();
client.watchAsync( "test_array" )
then( ( ) =>
multi.set( "test_array", "[1, 2]" )
.get( "test_array" )
.execAsync( )
)
.then( console.log ); // [ 0, "[1, 2]" ]
有关此文档的文档确实很少,但是我希望这个问题对将来的人有所帮助。
如果您日后阅读node_redis
,现在就可以享受我对node_redis
项目的个人贡献并查看更新的文档:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.