Node.js 有阻塞 redis 库吗?

发布于 2024-11-09 08:53:29 字数 261 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

稍尽春風 2024-11-16 08:53:29

我想您想确保所有 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.

剩余の解释 2024-11-16 08:53:29

对于试图习惯 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

誰認得朕 2024-11-16 08:53:29

阻塞代码会造成巨大瓶颈。

如果您使用阻止代码,您的服务器将变得令人难以置信缓慢。

请记住,节点是单线程的。因此,任何阻塞代码都会阻塞每个连接的客户端的节点。

您自己的基准测试表明它对于一个客户端来说已经足够快了。您是否已与 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

多情出卖 2024-11-16 08:53:29

虽然 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文