How to use redis to store hierarchical data?

前端 未结 1 924
无人共我
无人共我 2021-02-19 14:01

I have a set of hierarchical data to store, the hierarchy is like site/building/floor, the data, for example

{ 
   site:\'New York\',
   buildings: [
                    


        
1条回答
  •  北荒
    北荒 (楼主)
    2021-02-19 14:29

    In Redis you won't store your data in a unique data structure. You have to create multiple data structure, each one being identified by a key.

    Use a convention to name yours keys: by example site::buildings will be a set that contains the list of building ids for a given site.

    Then define hashes to store each building description. The key for these hashes could be something like: building:

    In the hash you have 2 members: name and floors. Floors value is the unique id of the set containing the list of floor identifiers.

    Then create a last set for each floor, to store the room names. The name of the sets could be something like: floor:.

    Tips:

    • use redis INCR command to generate unique IDs.
    • avoid too long keys if you intend to store a very high number of them (longer keys require more memory)

    0 讨论(0)
提交回复
热议问题