douyijin7741 2017-09-26 08:51
浏览 1016

在Redis中存储树结构

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":[

             ]
          }
       ]
    }
 ]
  • 写回答

1条回答 默认 最新

  • duanbipu1720 2017-09-26 12:22
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog