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

在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 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多