【问题标题】:Can i create a table like structure in redis like "ID , KEY , DATA , STATUS , EMAIL"我可以在redis中创建一个类似“ID、KEY、DATA、STATUS、EMAIL”的结构的表吗
【发布时间】:2019-01-25 13:52:59
【问题描述】:

我对 redis 很陌生。我们有一个要求,我需要在 redis 中创建一个类似结构的表。

ID |姓名 |地区 |时间
01 |啊 | s1 | ...
02 | BB | s2 | ...

  1. 我需要为 Primary Key(ID,Name,Region) 添加约束

【问题讨论】:

  • 您可能想要一个哈希 redis.io/topics/data-types,其中 redis 中的键是您需要的主键(您可能必须在 redis 之外计算这个值,因为您希望它基于值的组合)

标签: redis spring-data-redis


【解决方案1】:

您可以创建一个哈希映射存储表的所有行,键名应为主键。例如

// key should be the primary key
hmset user:1:Aaa:s1 id 1 name Aaa Region s1 Time 12:00
hmset user:2:Bbb:s2 id 2 name Bbb Region s2 Time 11:00

键名是主键user:1:Aaa:s1
如果要检索具有不同名称的密钥,则必须创建密钥并指向此哈希。

例如:

// If you want to retrieve with the userId.
set user:1 user:1:Aaa:s1
set user:2 user:2:Bbb:s2

【讨论】:

  • 有没有办法按名称检索数据?
  • 是的,因为名称是重复的,所以你必须像 user:Aaa 这样包含 hashIds user:1:Aaa:s1 的集合乙>。命令是sadd user:Aaa:user:1:Aaa:s1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多