【问题标题】:Best way to store <Integer, Integer> KVP in Redis for lookup by both Key and Value在 Redis 中存储 <Integer, Integer> KVP 以通过键和值查找的最佳方式
【发布时间】:2020-04-07 11:32:38
【问题描述】:

我想存储我自己的内部整数 ID 和外部系统 ID 的映射。比如我可能有以下数据:

MySystemInteger: 3, ExternalSystemInteger: 5
MySystemInteger: 8, ExternalSystemInteger: 1
MySystemInteger: 4, ExternalSystemInteger: 2

我希望能够为特定的 ExternalSystemInteger 值获取 MySystemInteger,并为特定的 MySystemInteger 值获取 ExternalSystemInteger。

我做了一些研究,虽然 redis 的 KEYS 功能可以让我在键空间中搜索特定值的值,但 KEYS 会遍历整个键空间以查找匹配项。由于 Redis 是单线程的,因此在此进程运行期间,对所有数据库的访问都将被阻止。

有什么好办法吗?还是我必须存储两个数据集?一种以 MySystemInteger 为键,另一种以 ExternalSystemInteger 为键?谢谢

【问题讨论】:

  • 如果此类关系的总数很小,您建议的两种索引方式的解决方案将起作用,如果此类关系以百万计,您可以考虑使用 hset 它们非常有效 寻找 hset 优化周围有很多主题.如果您分享更多信息,可以提出更好的解决方案。

标签: redis jedis key-value-store


【解决方案1】:

在生产中执行KEYS 是有风险的,正如redis documentation 中提到的那样,它可能会毁掉。在这种情况下,您可以使用哈希;

  • hset pair my:1 ext:7
  • hset pair ext:7 my:1
  • hget pair ext:7 返回my:1
  • hget pair my:1 返回ext:7

如果您愿意,可以在设置这两个哈希字段时使用hmset。将它们存储在不同的哈希中也是另一种选择。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多