69

Lets say I have a hash of a hash e.g.

$data = {
    'harry' : {
         'age' : 25,
         'weight' : 75,
    },
    'sally' : {
        'age' : 25,
        'weight' : 75,
    }
}
  1. What would the 'usual' way to store such a data structure (or would you not?)
  2. Would you be able to directly get a value (e.g. get harry : age ?
  3. Once stored could you directly change the value of a sub key (e.g. sally : weight = 100)
Ankur
  • 2,171
  • 23
  • 29
Xrender
  • 1,425
  • 4
  • 20
  • 25

3 Answers3

42

What would the 'usual' way to store such a data structure (or would you not?)

For example harry and sally would be stored each in separate hashes where fields would represent their properties like age and weight. Then set structure would hold all the members (harry, sally, ...) which you have stored in redis.

Would you be able to directly get a value (e.g. get harry : age ?)

Yes, see HGET or HMGET or HGETALL.

Once stored could you directly change the value of a sub key (e.g. sally : weight = 100)

Yes, see HSET.

yojimbo87
  • 65,684
  • 25
  • 123
  • 131
  • 2
    Thanks - so it would have to be stored in multiple operations? e.g. I could not store the data structure in one go? Obviously this was just a rather trivial example would want to store more complex 'objects' and perhaps am not understanding if this is the right way to go? – Xrender Jan 11 '12 at 08:55
  • 1
    Whole redis is based on simple operations among it's advanced data structures. You can try to look at [ohm](http://ohm.keyvalue.org/) which may abstract some things for you. – yojimbo87 Jan 11 '12 at 09:06
  • 2
    You can also write your own scripts in [Lua](http://redis.io/commands/eval); an application can send a Lua script as though it's an EVAL query and it will execute on the Redis server, letting your Lua script run multiple Redis commands without multiple round trips over the network. – awhie29urh2 Mar 25 '13 at 02:54
23

Lets take a complex data that we have to store in redis , for example this one:

  $data = { 
            "user:1"  : {
                       name : "sally",
                       password : "123"
                       logs : "25th october" "30th october" "12 sept",
                       friends : "34" , "24", "10"
                   } 
            "user:2"  :{
                       name : ""
                       password : "4567"
                       logs :
                       friends: ""
                   }
          }

The problem that we face is that the friends & logs are lists. So what we can do to represent this data in redis is use hashes and lists something like this :

Option 1. A hash map with keys as user:1 and user:2

      hmset user:1 name "sally" password "12344"
      hmset user:2 name "pally" password "232342"
      create separate list of logs as 
              logs:1 { here 1 is the user id }
              lpush logs:1 "" "" "" 
              lpush logs:2 "" "" ""
      and similarly for friends.

Option 2: A hash map with dumped json data as string encode

      hmset user:1 name "sally" password "12344" logs "String_dumped_data" friends "string of dumped data"

Option 3: This is another representation of #1

      something like user:1:friends -> as a list 
      and            user:2:friends -> as a list 

Please , correct me if i m wrong.

mareenator
  • 351
  • 2
  • 4
  • Yea as a Starter in Redis those are the solutions i could think of. But a consideration being one step further is querying. Lets say you need to get all logs of all users who have "24" as a "Friend". In case 1 you have to read all lists of friends and gather user id's and then get all logs. (=Mandatory Round trip) In case 2 i have no idea if redis has the ability to query inside dumped json data. And what the performance would be. And to Add a 3rd case lets say "Friends" was a more constant data type like "Rights" Would a structure like this make sence? user:21:friends:23-43-12 - > as List – Anestis Kivranoglou Oct 23 '14 at 10:37
  • For this case we can use EVAL command for internal lua processing – Alex Shwarc Nov 29 '17 at 15:14
3

Depends on what you want to do, but if your datastructure is not deeper nested and you need access to each field, I would recommend using hashes: http://redis.io/commands#hash

Here is a good overview over the redis datatypes, each with pro and contra: http://redis.io/topics/data-types

Max
  • 15,693
  • 14
  • 81
  • 131