如何修复 Redis 中 HSET 的参数错误?

3

我正在使用Redis在NodeJS和MongoDB中实现缓存层。我对Redis比较陌生,因此在尝试在一段时间后自动清除缓存时遇到了问题。我遇到的错误是:

ReplyError: ERR wrong number of arguments for 'hset' command

这是我的代码块。
mongoose.Query.prototype.exec = async function() {

  const key = JSON.stringify(
      Object.assign({}, this.getQuery(), {collection: 
      this.mongooseCollection.name})
  );
  const cachedValue = await client.hget(this.hashKey, key);

  if(cachedValue) {
      const parsedDoc = JSON.parse(cachedValue);

      return Array.isArray(parsedDoc) ? parsedDoc.map(doc => new 
      this.model(doc)) : new this.model(parsedDoc);
  }

  const result = await exec.apply(this, arguments);

  client.hset(this.hashKey, key, JSON.stringify(result), 'EX', 10);

  return result;
}
1个回答

8

2
自从 Redis 4.0.0 版本起,HMSET 被认为是不推荐使用的。请在新代码中使用 HSET。那么我们应该如何使用 HSET 呢? - fredmaggiowski
2
@FredMaggiowski,HSET现在可以像HMSET一样接受多个字段和值。 - Stephen Horvath

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接