skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. 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 })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search