使用ioredis为redis数据库增加id并将其分配给string

我在使用redis的博客上工作,并陷入了api级别。 我正在尝试做以下事情:

MULTI INCR id SET post:{id} json_value //Stucked here SADD posts {id} EXEC 

那么如何获得SET文章的ID:{id}?

我现在有下面的代码,这是不行的。

 // Create post function cpost(json) { client.pipeline() .incr('id')) .set('post:' + client.get('id:post', function (err, results) { return results; }), json) .sadd('posts, client.get('id:post', function (err, results) { return results; })) // posts post .exec(function (err, results) { }); } 

使用ioredis

任何想法如何获得身份证的价值?

如果你想有一个事务,你应该使用multi()而不是pipeline() (我认为是这种情况)。

以下是我处理这个问题的方式。

我只能用承诺的两个步骤做到这一点。 你也可以使用callback来达到同样的效果。 无论哪种方式增量超出了交易或批次。

 function cpost(json) { client.incr('id', 'post').then(function (postId) { client.multi() .set('blog:post:' + postId, json) .sadd('blog:posts', postId) .exec() .then( function onSuccess(data) { // success handling goes here }, function onError(data) { // error handling goes here } ); } 

我不认为这是解决我的问题的正确方法,但我必须继续前进。 最简单的解决scheme(计数器):

 var id = 0; function cpost(json) { id++; client.pipeline() .incr('id', 'post') .set('blog:post:' + id, json) // blog:post:1 .sadd('blog:posts', id) // blog:posts id:post .exec(function (err, results) { }); }