3

I try to use redis sorted set command zadd. But keeping throw error when i run this script:

var ts = Math.round(Date.now() / 1000)
      , key = 'usr::' + dest.ID + '::msgs'
      , id = uuid.v1();
var notify = {
    msg: response.msg,
    from: response.from ? response.from : null,
    type: response.type ? response.type : null,
    date: ts,
    read: 0
}
client.zadd(key, ts, JSON.stringify(notify), function (err, response) {
    if (err) throw err;
});

Is anything wrong with this code?

By the way: What i try to accomplish is notification/inbox system... So better save time from asking me like you will help and finally you don't :(

ERROR: ERR Operation against a key holding the wrong kind of value

4

1 回答 1

2

我会说密钥已经存在于 Redis 中并且不是排序集。尝试查看 Redis 中是否已有 usr::ID::msgs 条目,并检查它们的类型。

更新:

如果只保留一个排序集,则实际上不可能更新条目,因为条目数据被序列化并用作排序集项的值。

不过,您有几种解决方案:

1)您可以读取和删除项目,反序列化,更改读取状态,再次序列化,再次将项目添加到排序集中。如果需要,可以使用服务器端 Lua 脚本一次往返完成。

2)您可以将数据模型拆分为多个对象:保留一组关联时间戳和消息 ID 的排序集,并为每个消息 ID 使用一个哈希对象来存储每条消息的属性。因此,更新消息的读取状态很容易(HMSET)。

3)您还可以有两个排序集(一个用于已读消息,一个用于未读消息)。更改消息的状态将涉及从一组中删除该项目,并将其添加到另一组中。

解决方案的最佳选择可能取决于您的数据访问模式。

于 2014-02-18T18:01:10.253 回答