Node.js 有阻塞 redis 库吗?
Redis 非常快。在我的机器上的大部分情况下,它与 Node.js 中的原生 Javascript 语句或函数调用一样快。在 Node.js 中编写常规 Javascript 代码很容易/无痛,因为不需要回调。我不明白为什么使用 node.js 在 Redis 中获取/设置键/值数据不那么容易。
假设 Node.js 和 Redis 在同一台机器上,是否有任何 npm 库允许使用阻塞调用与 Node.js 上的 Redis 进行交互?我知道这必须是一个与 V8 接口的 C/C++ 库。
Redis is very fast. For most part on my machine it is as fast as say native Javascript statements or function calls in node.js. It is easy/painless to write regular Javascript code in node.js because no callbacks are needed. I don't see why it should not be that easy to get/set key/value data in Redis using node.js.
Assuming node.js and Redis are on the same machine, are there any npm libraries out there that allow interacting with Redis on node.js using blocking calls? I know this has to be a C/C++ library interfacing with V8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想您想确保所有 redis 插入操作都已执行。为此,您可以使用 MULTI 命令插入密钥或执行其他操作。
https://github.com/mranney/node_redis 模块将多对象中推送的命令排队,并且相应地执行它们。
这样,您只需要在 exec 调用结束时进行一次回调。
I suppose you want to ensure all your redis insert operations have been performed. To achieve that, you can use the MULTI commands to insert keys or perform other operations.
The https://github.com/mranney/node_redis module queues up the commands pushed in multi object, and executes them accordingly.
That way you only require one callback, at the end of exec call.
对于试图习惯 Node 的事件编程模型的开发人员来说,这似乎是一个常见的陷阱。
发生的情况是这样的:您遇到了异步/回调模式不太适合的情况,您认为您需要的是某种执行阻塞代码的方法,您向 Google/StackExchange 询问有关 Node 中的阻塞的信息,您得到的只是是对封锁有多糟糕的警告。
他们是对的——阻塞(“在做其他事情之前等待结果”)不是你应该在 Node 中尝试做的事情。但我认为更有帮助的是要认识到 99.9% 的情况下,你并不是真的在寻找一种方法来进行阻塞,你只是在寻找一种方法来制作你的应用程序,“等待这个结果在继续这样做之前”,这并不是完全相同的事情。
尝试研究 Node 中的“流控制”而不是“阻塞”的一些设计模式,这可能更适合您想要做的事情。以下是要查看的库列表:
https://github.com/ Joyent/node/wiki/modules#wiki-async-flow
我也是 Node 新手,但我真的很喜欢异步:https://github.com/caolan/async
This seems like a common bear-trap for developers who are trying to get used to Node's evented programming model.
What happens is this: you run into a situation where the async/callback pattern isn't a good fit, you figure what you need is some way of doing blocking code, you ask Google/StackExchange about blocking in Node, and all you get is admonishment on how bad blocking is.
They're right - blocking, ("wait for the result of this before doing anything else"), isn't something you should try to do in Node. But what I think is more helpful is to realize that 99.9% of the time, you're not really looking for a way to do blocking, you're just looking for a way to make your app, "wait for the result of this before going on to do that," which is not exactly the same thing.
Try looking into the idea of "flow control" in Node rather than "blocking" for some design patterns that could be a clearer fit for what you're trying to do. Here's a list of libraries to check out:
https://github.com/joyent/node/wiki/modules#wiki-async-flow
I'm new to Node too, but I'm really digging Async: https://github.com/caolan/async
阻塞代码会造成巨大瓶颈。
如果您使用阻止代码,您的服务器将变得令人难以置信缓慢。
请记住,节点是单线程的。因此,任何阻塞代码都会阻塞每个连接的客户端的节点。
您自己的基准测试表明它对于一个客户端来说已经足够快了。您是否已与 1000 个客户进行了基准测试?如果您尝试这样做,您就会明白为什么阻塞代码是不好的
Blocking code creates a MASSIVE bottleneck.
If you use blocking code your server will become INCREDIBLY slow.
Remember, node is single threaded. So any blocking code, will block node for every connected client.
Your own benchmarking shows it's fast enough for one client. Have you benchmarked it with a 1000 clients? If you try this you will see why blocking code is bad
虽然 Redis 速度很快,但它不是瞬时的……这就是为什么如果您想继续执行以确保值存在,则必须使用回调。
我认为您可以(并且不建议您这样做)实现此目的的唯一方法是使用带有变量的回调,该变量是离开计时器的谓词。
Whilst Redis is quick it is not instantaneous ... this is why you must use a callback if you want to continue execution ensuring your values are there.
The only way I think you could (and am not suggesting you do) achieve this use a callback with a variable that is the predicate for leaving a timer.