问题
i have a question about HSET in redis. As far as i know, redis is a key-value database. that means every thing store as a key-value and we don't have table for example. i wanted to save something in redis so i decided to use Hashmap . since the HMSET is deprecated and we should use HSET instead, how should I store many attribute as a value and a id as a key in hset? you know i want to save some thing like this:
await redis.hset(`origin-${originId}`, 'title',title)
but if i have many fields to save i should write this line for each field?? for example :
await redis.hset(`origin-${originId}`, 'title',title)
await redis.hset(`origin-${originId}`, 'status',status)
...
as in HSET we should define 3 parameter i write this code. Isn't there a better solution?
回答1:
you can do this
hset key field1 value1 field2 value2 ...
It's documented here
https://redis.io/commands/hmset and https://redis.io/commands/hset
but hmset is deprecated and you should use hset now because it supports the same args as hmset
in node.js, if you already have the key / value in an object, you can do this
await redis.hset('myhashset', { field1: value1, field2: value2 })
回答2:
i figure out that my problem was with my redis client. my redise version is 5 but my redis client doesn't support new features in Redis 5. so i use ioredis and this redis client support hmset now. also there is a fix for that issue but wasn't released yet see enter link description here
来源:https://stackoverflow.com/questions/59352905/how-can-store-a-json-in-redis-with-hashmap-hset