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

Raj*_*rik 2 javascript redis node.js

我正在使用 Redis 在 NodeJS 和 MongoDB 中实现缓存层。我对Redis相当陌生。所以我在尝试在给定时间后自动清除缓存时遇到了麻烦。我得到的错误

ReplyError: ERR wrong number of arguments for 'hset' command
Run Code Online (Sandbox Code Playgroud)

这是我的代码块

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;
}
Run Code Online (Sandbox Code Playgroud)

Fai*_*med 5

RedisHSET只接受 3 个参数。如果要在一次调用中存储多个密钥,则应使用HMSET.

参考:

https://redis.io/commands/hset

https://redis.io/commands/hmset

client.hmset(this.hashKey, key, JSON.stringify(result), 'EX', 10);
Run Code Online (Sandbox Code Playgroud)

应该管用。

  • 由于[“根据Redis 4.0.0,HMSET被视为已弃用。请在新代码中使用HSET。”](https://redis.io/commands/hmset)我们应该如何使用hset? (2认同)
  • @FredMaggiowski HSET 现在可以接受多个字段和值,就像 HMSET 过去一样。 (2认同)