栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Redis中建立多对多关系

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Redis中建立多对多关系

使用Redis,关系通常由集合表示。一组可用于表示单向关系,因此每个对象需要一组以表示多对多关系。

尝试将关系数据库模型与Redis数据结构进行比较是毫无用处的。使用Redis,所有内容均以非规范化方式存储。

例:

# Here are my categories> hmset category:1 name cinema  ... more fields ...> hmset category:2 name music   ... more fields ...> hmset category:3 name sports  ... more fields ...> hmset category:4 name nature  ... more fields ...# Here are my users> hmset user:1 name Jack   ... more fields ...> hmset user:2 name John   ... more fields ...> hmset user:3 name Julia  ... more fields ...# Let's establish the many-to-many relationship# Jack likes cinema and sports# John likes music and nature# Julia likes cinema, music and nature# For each category, we keep a set of reference on the users> sadd category:1:users 1 3> sadd category:2:users 2 3> sadd category:3:users 1> sadd category:4:users 2 3# For each user, we keep a set of reference on the categories> sadd user:1:categories 1 3> sadd user:2:categories 2 4> sadd user:3:categories 1 2 4

一旦有了此数据结构,就可以使用集合代数查询它:

# Categories of Julia> smembers user:3:categories1) "1"2) "2"3) "4"# Users interested by music> smembers category:2:users1) "2"2) "3"# Users interested by both music and cinema> sinter category:1:users category:2:users1) "3"


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/381635.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号