在redis中处理层次结构

Handling hierarchical structure in redis

大家晚上好, 我有一个项目的树结构,像这样:

[
    {
        "categoryId": "44eec2a3-7ca5-4e27-ab0f-3294a4fdad45",
        "categoryName": "Parent Category",
        "categoryParentId": "44eec2a3-7ca5-4e27-ab0f-3294a4fdad45",
        "children": [
            {
                "categoryId": "cd6df4dc-a117-45c6-9a41-8a4293556042",
                "categoryName": "Sub Category 1",
                "categoryParentId": "44eec2a3-7ca5-4e27-ab0f-3294a4fdad45",
                "children": [
                    {
                        "categoryId": "1d96fd93-dbfa-45cb-8a22-e05f47eca1ff",
                        "categoryName": "Sub Category 1_1",
                        "categoryParentId": "44eec2a3-7ca5-4e27-ab0f-3294a4fdad45"
                    }
                ]
            },
            {
                "categoryId": "4422b64e-b6e3-4d42-8cee-50c5b57e5f3d",
                "categoryName": "Sub Category 2",
                "categoryParentId": "44eec2a3-7ca5-4e27-ab0f-3294a4fdad45",
                "children": [
                    {
                        "categoryId": "bae56c0e-ad41-4c95-97ed-bece419794b5",
                        "categoryName": "Sub Category 2_1",
                        "categoryParentId": "44eec2a3-7ca5-4e27-ab0f-3294a4fdad45"
                    },
                    {
                        "categoryId": "3b02a7d8-3776-46eb-9872-b10c01f43962",
                        "categoryName": "Sub Category 2_2",
                        "categoryParentId": "44eec2a3-7ca5-4e27-ab0f-3294a4fdad45",
                        "children": [
                            {
                                "categoryId": "d123c7c7-535e-445b-87a4-a53f675e1aa6",
                                "categoryName": "Sub Category 2_2_1",
                                "categoryParentId": "44eec2a3-7ca5-4e27-ab0f-3294a4fdad45"
                            }
                        ]
                    }
                ]
            },
            {
                "categoryId": "86ec623d-47fe-43cc-bdb5-dd834d4bd0a2",
                "categoryName": "Sub Category 3",
                "categoryParentId": "44eec2a3-7ca5-4e27-ab0f-3294a4fdad45",
                "children": [
                    {
                        "categoryId": "a82dc6ef-72f8-46d8-ac1e-738b7145b1ce",
                        "categoryName": "Sub Category 3_1",
                        "categoryParentId": "44eec2a3-7ca5-4e27-ab0f-3294a4fdad45",
                        "children": [
                            {
                                "categoryId": "592fbff7-33ea-41ce-b082-3862fe92ef49",
                                "categoryName": "Sub Category 3_1_1",
                                "categoryParentId": "44eec2a3-7ca5-4e27-ab0f-3294a4fdad45"
                            }
                        ]
                    },
                    {
                        "categoryId": "6c9e263f-ef3f-4eb3-8d5b-c07bba4459a7",
                        "categoryName": "Sub Category 3_2",
                        "categoryParentId": "44eec2a3-7ca5-4e27-ab0f-3294a4fdad45",
                        "children": [
                            {
                                "categoryId": "ee68f439-b863-43a4-8df4-c347b575218a",
                                "categoryName": "Sub Category 3_2_1",
                                "categoryParentId": "44eec2a3-7ca5-4e27-ab0f-3294a4fdad45"
                            }
                        ]
                    }
                ]
            },
            {
                "categoryId": "7b18a6c7-a4c9-41cc-aadc-e0abde15662d",
                "categoryName": "Sub parent",
                "categoryParentId": "44eec2a3-7ca5-4e27-ab0f-3294a4fdad45"
            },
            {
                "categoryId": "a527966a-0f1d-41f1-a8e8-bd0552687f03",
                "categoryName": "Sub parent",
                "categoryParentId": "44eec2a3-7ca5-4e27-ab0f-3294a4fdad45"
            }
        ]
    }
]

这棵树是一键存储在redis中的。 鉴于 ID 为 (ee68f439-b863-43a4-8df4-c347b575218a) 的类别已在 SQL 数据库中更新(例如:MySQL),那么如何告诉 Redis 更新此元素而不是刷新缓存并重新设置? 从数据库中删除项目时的相同情况。

来自命令 SET 的 redis 文档。

Set key to hold the string value. If key already holds a value, it is overwritten, regardless of its type.

要删除密钥,您可以使用 DEL

Removes the specified keys. A key is ignored if it does not exist.

除此之外,如果你想更新 JSON 中的项目,你需要从 redis GET 当前的 JSON,解析它,更新它,然后 SET 它回到 redis 存储。