Reputation: 4682
I have a tree structure like the following, which is populated using PHP. Populating the complete structure everytime is time-consuming and so I would like to put it in Redis. Now it is stored as plain JSON, but then again after adding a new item, I need to delete the existing JSON and need to populate and insert to Redis again. So I would like to have a structure so that I can add a new item in any hierarchy, that is either as a parent item or a child. Which data structure in Redis I can use for this and how can I do it?
[
{
"id":2,
"order":2,
"children":[
{
"id":3,
"order":1,
"children":[
],
"actions":[
{
"id":1,
"slug":"manage",
"title":"manage",
"api":[
]
}
]
},
{
"id":4,
"order":2,
"children":[
],
"actions":[
{
"id":2,
"slug":"settings",
"title":"settings",
"api":[
]
}
]
}
],
"actions":[
{
"id":190,
"slug":"update",
"title":"update",
"api":[
]
}
]
}
]
Upvotes: 1
Views: 2907
Reputation: 49942
Redis' core data structures do not have nesting, so the short answer is that you can't do that easily.
As you've correctly noted, storing the serialized JSON has the disadvantage of mandating full read/write of the values, even if just a subset is touched.
The two alternatives I can recommend are a) using Lua to manipulate the serialized JSON or b) using ReJSON.
Disclaimer: I'm ReJSON's author.
Upvotes: 3