如何使用redis来存储分层数据? [英] How to use redis to store hierarchical data?

查看:209
本文介绍了如何使用redis来存储分层数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组层次数据要存储,层次就像站点/建筑/楼层,数据,例如

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

{ 
   site:'New York',
   buildings: [
              {
                name:'building a',
                floors: [
                       'Ground':[{room:room1},{room:room2}],
                       'First':[{room:room1},{room:room2}]
                        ]
              }
          ] 
},
{ 
   site:'London',
   buildings: [
              {
                name:'building a',
                floors: [
                       'Ground':[{room:room1},{room:room2}],
                       'First':[{room:room1},{room:room2}]
                        ]
              }
          ] 
}

我想将这些房间数据存入一个集合中,但我也可以通过选择站点名称或(站点名称+建筑物名称)或(站点名称+建筑物名称+楼层)来查询房间的子集

I want to store these room data into a set, but I can also query the a subset of rooms by selecting the site name or (site name + building name ) or ( site name + building name + floor )

推荐答案

在 Redis 中,您不会将数据存储在唯一的数据结构中.您必须创建多个数据结构,每个数据结构由一个键标识.

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.

使用约定来命名您的密钥:例如,site:<CITY>:buildings 将是一个包含给定站点的建筑物 ID 列表的集合.

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

然后定义散列来存储每个建筑物描述.这些散列的密钥可能类似于:building:

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

在散列中,您有 2 个成员:姓名和楼层.Floors 值是包含楼层标识符列表的集合的唯一 ID.

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.

然后为每个楼层创建最后一组,以存储房间名称.集合的名称可能类似于:floor:.

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

提示:

  • 使用 redis INCR 命令生成唯一 ID.
  • 如果您打算存储大量密钥,请避免太长的密钥(更长的密钥需要更多内存)

这篇关于如何使用redis来存储分层数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆