Node.js、(Hi)Redis 和 multi 命令 [英] Node.js, (Hi)Redis and the multi command

查看:123
本文介绍了Node.js、(Hi)Redis 和 multi 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 node.js 和 redis 并通过此命令安装了hiredis 库

I'm playing around with node.js and redis and installed the hiredis library via this command

npm install hiredis redis

我在这里查看了多个示例:

I looked at the multi examples here:

https://github.com/mranney/node_redis/blob/master/examples/multi2.js

在第 17 行,它说

At line 17 it says

// you can re-run the same transaction if you like

这意味着一旦命令完成执行,内部 multi.queue 对象就永远不会被清除.

which implies that the internal multi.queue object is never cleared once the commands finished executing.

我的问题是:您将如何处理 http 环境中的情况?例如,跟踪最后连接的用户(这并不真正需要 multi 因为它只执行一个命令但很容易遵循)

My question is: How would you handle the situation in an http environment? For example, tracking the last connected user (this doesn't really need multi as it just executes one command but it's easy to follow)

var http = require('http');
redis = require('redis');

client = redis.createClient()
multi = client.multi();

http.createServer(function (request, response) {
  multi.set('lastconnected', request.ip); // won't work, just an example
  multi.exec(function(err, replies) {
      console.log(replies);
  });
});

在这种情况下,multi.exec 将为第一个连接的用户执行 1 个事务,并为第 100 个用户执行 100 个事务(因为内部 multi.queue 对象永远不会被清除).

In this case, multi.exec would execute 1 transaction for the first connected user, and 100 transactions for the 100th user (because the internal multi.queue object is never cleared).

选项 1: 我应该在 http.createServer 回调函数内创建多对象,这会在函数执行结束时有效地终止它吗?就 CPU 周期而言,创建和销毁该对象的代价有多大?

Option 1: Should I create the multi object inside the http.createServer callback function, which would effectivly kill it at the end of the function's execution? How expensive in terms of CPU cycles would creating and destroying of this object be?

选项 2: 另一个选项是创建一个新版本的 multi.exec(),类似于 multi.execAndClear() ,它会在 redis 执行的那一刻清除队列 一堆命令.

Option 2: The other option would be to create a new version of multi.exec(), something like multi.execAndClear() which will clear the queue the moment redis executed that bunch of commands.

你会选择哪个?我认为选项 1 更好 - 我们杀死一个对象而不是挑选它的一部分 - 我只是想确定一下,因为我对 node 和 javascript 都是全新的.

Which option would you take? I suppose option 1 is better - we're killing one object instead of cherry picking parts of it - I just want to be sure as I'm brand new to both node and javascript.

推荐答案

node_redis 中的多对象的创建成本非常低.作为副作用,我认为让您重用它们会很有趣,但这显然仅在某些情况下才有用.每次需要新事务时,继续创建一个新的多对象.

The multi objects in node_redis are very inexpensive to create. As a side-effect, I thought it would be fun to let you re-use them, but this is obviously only useful under some circumstances. Go ahead and create a new multi object every time you need a new transaction.

要记住的一件事是,只有当您确实需要在 Redis 服务器中以原子方式执行所有操作时,才应该使用 multi.如果您只想高效地批量处理一系列命令以节省网络带宽并减少您必须管理的回调数量,只需发送单个命令,一个接一个.node_redis 会自动将这些请求按顺序管道化"到服务器,并且各个命令回调(如果有)将按顺序调用.

One thing to keep in mind is that you should only use multi if you actually need all of the operations to execute atomically in the Redis server. If you just want to batch up a series of commands efficiently to save network bandwidth and reduce the number of callbacks you have to manage, just send the individual commands, one after the other. node_redis will automatically "pipeline" these requests to the server in order, and the individual command callbacks, if any, will be invoked in order.

这篇关于Node.js、(Hi)Redis 和 multi 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆