Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested hash in redis

Tags:

ruby

redis

hash

I want to use a hash of hash (nested hash). For example,

{Key 1 -> 
  {Subkey 1 -> {Value1, Value2...}, 
    Subkey 2 -> {Value1, Value2...},
    .
    .
    Subkey n -> {Value1, Value2...}
  }
  Key 2 -> {...}
  .
  .
  Key n -> {...}
}

Tell me how I can define this structure in redis and also how to access these values. I am trying to use this on ruby.

like image 659
Paras Meena Avatar asked Oct 29 '25 22:10

Paras Meena


2 Answers

you can't use nested hash in redis, but in the kind of situation you are asking you can use two hashes, one for key to subkey and the other one for subkey to your values.

like image 154
whishky Avatar answered Oct 31 '25 11:10

whishky


If you just want to store and retrieve them and you don't need the hash functions in Redis, you can just serialize the subhashes (e.g. Marshal, JSON, YAML).

Otherwise consider a different key-value database, like MongoDB; or @whishky's answer.

like image 35
Leventix Avatar answered Oct 31 '25 12:10

Leventix