我正在尝试使用 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/