node.js - Node 和 Redis 队列

标签 node.js redis queue node-redis

我正在尝试使用 Redis 创建一个 JSON 对象队列。

我目前使用 ZADD 创建有序集:

var entry = {"name": "Hank", "question": "Where am I?"};
client.zadd("entries", 1, JSON.stringify(entry));

如何在每次有新条目时增加分数?

最佳答案

如果您正在尝试创建一个队列,其中新项目被添加到队列的顶部,那么您可能根本不需要排序集。

Sorted Sets 在您有一个具有不同分数的事物列表时非常有用,例如视频游戏的排行榜,或最佳居住地的排名。如果你问如何增加现有条目的分数,那很简单,你只需用新分数再次添加它即可。因此,client.zadd("entries", 2, JSON.stringify(entry)); 会将分数更新为 2。来自 redis.io :

Adds all the specified members with the specified scores to the sorted set stored at key. If a specified member is already a member of the sorted set, the score is updated and the element reinserted at the right position to ensure the correct ordering.

听起来您好像在问如何在向集合中添加新项目时增加所有现有项目的分数。这就像将每个现有项目“推”到一个位置并将新项目放在顶部一样。这不是排序集的用途,而正是 redis 列表的用途。而且,插入正是你会做的。使用 LPUSH在列表中添加元素。

关于node.js - Node 和 Redis 队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19328875/

相关文章:

python - 确定 Python JoinableQueue 中有多少项

javascript - 下载人物头像并在node.js中展示

javascript - Electronjs 应用程序 async/await 不等待函数返回数据

node.js - 使用嵌套对象填充嵌套 Mongoose 对象

node.js - 无服务器 AWS Lambda 边缘 : MalformedHandlerName

database - 在键值存储中管理键的好方法是什么?

.net - 有没有办法获取队列中的最后一个元素?

redis - twemproxy(nutcracker),错误 : Protocol error, 得到 "{"作为回复类型字节

redis - 如何计算匹配模式的键数?

C 队列清理指针