【问题标题】:how can store a Json in redis with hashmap( HSET )如何使用hashmap(HSET)将Json存储在redis中
【发布时间】:2019-12-16 08:31:46
【问题描述】:

我对 redis 中的 HSET 有疑问。据我所知,redis 是一个键值数据库。这意味着每件事都存储为键值,例如我们没有表。 我想在 redis 中保存一些东西,所以我决定使用 Hashmap 。由于 HMSET 已弃用,我们应该改用 HSET,我应该如何将许多属性作为值存储,将 id 作为键存储在 hset 中? 你知道我想保存这样的东西:

await redis.hset(`origin-${originId}`, 'title',title)

但是如果我有很多字段要保存,我应该为每个字段写下这一行??例如:

await redis.hset(`origin-${originId}`, 'title',title)
await redis.hset(`origin-${originId}`, 'status',status)
...

在 HSET 中,我们应该定义 3 个参数,我编写此代码。没有更好的解决方案吗?

【问题讨论】:

  • 你检查 redisjson.io 了吗?
  • @GuyKorland 不,我应该如何在我的示例中使用它?
  • 你经历过this吗?
  • @Praga_t 这是支持 hmset 的旧版本。但我的问题在于 redis 版本 5 中的 hset

标签: node.js redis hashmap


【解决方案1】:

你可以这样做

hset key field1 value1 field2 value2 ...

这里有记录

https://redis.io/commands/hmsethttps://redis.io/commands/hset

hmset 已弃用,您现在应该使用hset,因为它支持与hmset 相同的参数

在node.js中,如果你已经在一个对象中有key/value,你可以这样做

await redis.hset('myhashset', { field1: value1, field2: value2 })

【讨论】:

  • 我这样做并得到这个错误:ReplyError: ERR wrong number of arguments for 'hset' command
  • 你可能使用的是旧版本的 redis。自从 redis 4 和当前版本是 5 以来就支持这一点。你能检查你的 redis 版本吗?如果不支持,您可以使用hmset
  • 我的Redis服务器版本是4.0.9,ioredis客户端是4.14.1
  • ioredis -> 使用hmset,对此有修复但尚未发布,请参阅 (github.com/luin/ioredis/pull/1012)
【解决方案2】:

我发现我的问题出在我的 redis 客户端上。我的 redis 版本是 5,但我的 redis 客户端不支持 Redis 5 中的新功能。 所以我现在使用 ioredis 并且这个 redis 客户端支持 hmset。还有针对该问题的修复程序,但尚未发布,请参阅enter link description here

【讨论】:

    猜你喜欢
    • 2021-12-13
    • 2020-06-29
    • 2015-12-08
    • 1970-01-01
    • 2019-09-16
    • 2017-06-12
    • 2013-05-08
    • 1970-01-01
    • 2020-02-29
    相关资源
    最近更新 更多