- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
所以,我仍然在很长一段时间内学习其他语言的 JS/Node 方式。
我有一个微型微服务,它从 Redis channel 读取数据,将其临时存储在工作 channel 中,完成工作,删除它,然后继续。如果 channel 中有更多内容,它会立即重新运行。如果没有,它会设置超时并在 1 秒后再次检查。
它工作正常...但超时轮询似乎不是解决此问题的“正确”方法。而且我还没有发现太多关于使用 BRPOPLPUSH 尝试阻止(相对于 RPOPLPUSH)并在 Node 中等待......或其他类似选项的信息。 (发布/订阅在这里不是一个选项......这是唯一的监听器,它可能并不总是在监听。)
这是我正在做的事情的简短要点:
var Redis = require('ioredis');
var redis = new Redis();
var redisLoop = function () {
redis.rpoplpush('channel', 'channel-working').then(function (result) {
if (result) {
processJob(result); //do stuff
//delete the item from the working channel, and check for another item
redis.lrem('channel-working', 1, result).then(function (result) { });
redisLoop();
} else {
//no items, wait 1 second and try again
setTimeout(redisLoop, 1000);
}
});
};
redisLoop();
我觉得我错过了一些非常明显的东西。谢谢!
最佳答案
BRPOPLPUSH
不会在 Node 中阻塞,它会在 client 中阻塞。在这种情况下,我认为这正是您摆脱轮询所需要的。
var Redis = require('ioredis');
var redis = new Redis();
var redisLoop = function () {
redis.brpoplpush('channel', 'channel-working', 0).then(function (result) {
// because we are using BRPOPLPUSH, the client promise will not resolve
// until a 'result' becomes available
processJob(result);
// delete the item from the working channel, and check for another item
redis.lrem('channel-working', 1, result).then(redisLoop);
});
};
redisLoop();
请注意 redis.lrem
是异步的,因此您应该使用 lrem(...).then(redisLoop)
来确保您的下一个 tick 仅在项目已成功从 channel-working
中移除。
关于node.js - 编写简单的 Node redis 循环(使用 ioredis)的更好方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40433423/
我正在遵循 Jest 文档 here 中的手动模拟示例 我正在尝试将此示例扩展到我自己的项目和 ioredis 的手动模拟 (mocks/ioredis.js)。我正在尝试用我自己的 ioredis
我正在遵循 Jest 文档 here 中的手动模拟示例 我正在尝试将此示例扩展到我自己的项目和 ioredis 的手动模拟 (mocks/ioredis.js)。我正在尝试用我自己的 ioredis
当使用 ioredis ( https://github.com/luin/ioredis ) 连接到 redis 集群时,您只需要指定一个节点,例如三节点集群 127.0.0.1:7000127.0
我正在尝试优雅地处理 Redis 错误,以便绕过错误并改为执行其他操作,而不是让我的应用程序崩溃。 但到目前为止,我不能只捕获 ioredis 抛出的异常,它绕过了我的 try/catch 并终止了当
我将 ioredis 与 express (nodejs) 一起使用我知道有一种方法可以通过这样的模式删除键: redis-cli KEYS "sample_pattern:*" | xargs re
我目前正在尝试从运行我的应用程序的服务器连接到存储在另一个实例上的我的 Redis 集群。我正在使用 IoRedis 在我的应用程序和我的 Redis 实例之间建立接口(interface),并且当只
这是我的代码: const Redis = require('ioredis'); const client = new Redis(); // multi set client.mset({'key
我正在使用ioredis。 为了防止巨大的缓冲区可能会使我的应用程序崩溃,我想在redis关闭时忽略请求,并捕获这些请求。 有什么办法可以实现? 最佳答案 您可以使用circuit breaker d
我想用键匹配模式“LOGIN::”搜索 Redis 数据库。我在我的应用程序中使用 ioredis。昨天我搜索了整个网络,我得到了一些可以完成这项工作的选项,如下所示: 按键 扫描流 问题: impo
我正在尝试为 ioredis nodejs 中的一组值设置过期时间。我可以使用此命令将一组添加到 reids redis.sadd('set', 1, 3, 5, 7); 但是我需要为这个集合设置一个
我很难在我的 typescript 测试中模拟第三方库。 我正在基于这个 typescript-starter 创建一个库图书馆。它使用 ava 进行测试。 在我的例子中,我试图模拟 ioredis
我正在使用 ioredis 模块。 var Redis = require('ioredis'); var redis = new Redis(); 在执行 new Redis() 时,与 Redis
在我设置或获取 key 之前,是否有任何方法可以检查 weather redis 是否可用或由于某些问题而终止?我怎样才能做到这一点? 我正在使用 ioredis 模块。 最佳答案 var Redi
const Redis = require('ioredis'); const sub = new Redis(); const pub = new Redis(); sub.on('subscrib
我将 ioredis 客户端 (@4.6.2) 与 node.js 一起使用,我需要做很多位操作(它们不相互依赖)。像这样: import * as ioredis from "ioredis"; .
我有一个基于 Laravel 构建的应用程序。我正在使用 Laradock 并尝试使用 Redis 容器,但我遇到了连接问题。 使用命令: docker inspect laradock_redis_
我正在使用 ioredis我想返回下面示例中的路径和值,一直到匿名函数。 console.log( function (jsonGraphArg) { return Redis
我正在尝试在 Node 脚本的 ioredis 中使用 hget 和 hset,我查看了文档,但找不到如何操作,知道如何操作吗? 谢谢, 最佳答案 这已在评论中得到解答,但对于 future 的搜索引
我想使用 Redis 位图来表示值。 如果我有一个整数值,那么我希望能够将位图的相应索引设置为 1或 true . 我还希望能够检索整个位图并遍历它以确定“1”的位置。 有一个bitfield dat
我们希望我们的 Redis 更具可扩展性,并且我们希望能够添加更多读取实例。 我正在尝试使用这个新的阅读器端点:https://aws.amazon.com/about-aws/whats-new/2
我是一名优秀的程序员,十分优秀!