Complex Redis Data Structures

Suppose I have a hash hash, for example.

$data = { 'harry' : { 'age' : 25, 'weight' : 75, }, 'sally' : { 'age' : 25, 'weight' : 75, } } 
  • What will be the โ€œnormalโ€ way of storing such a data structure (or don't you want?)
  • Could you get the value directly (e.g. get harry: age?
  • After saving, you can directly change the value of the auxiliary key (for example, sally: weight = 100)
+48
redis
Jan 10 '12 at 20:26
source share
3 answers

What will be the โ€œnormalโ€ way of storing such a data structure (or are you not?)

For example, harry and sally will each be stored in separate hashes , where the fields will represent their properties, such as age and weight. Then the set structure will contain all the members (harry, sally, ...) that you saved in redis.

You could get the value directly (e.g. get harry: age?)

Yes, see HGET or HMGET or HGETALL .

After saving, you can directly change the value of the additional key (for example, sally: weight = 100)

Yes, see HSET .

+19
Jan 10 2018-12-12T00:
source share

Let's take the complex data that we need to store in redis, like 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 we are facing is that friends and magazines are lists. So what we can do to represent this data in redis is to use hashes and list something like this:

Option 1. Hash card 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: hash map with json hyphenated data as string encoding

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

Option 3: This is another view # 1

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

Please correct me if I am wrong.

+14
Aug 14 '13 at 8:52
source share

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

Here is a good overview on the types of redis, each of which has pro and contra: http://redis.io/topics/data-types

+2
Jan 10 2018-12-12T00:
source share



All Articles