简体   繁体   English

是否有可能在Redis中按值获取数据?

[英]Is it possible to get data by value in Redis?

I already checked to be able to find a row by key in Redis. 我已经检查过能够在Redis中按键找到一行。 But I wonder if it's possible to find a row by value in same row. 但我想知道是否可以在同一行中找到一行值。 For example, my row's data is {"1", "A", "B"} and I wanna find the row by "A" or "B" not by "1" (first columns is key in this case) with Python. 例如,我的行的数据是{“1”,“A”,“B”},我想通过“A”或“B”而不是“1”找到行(在这种情况下,第一列是关键) 。

Redis has nothing out of the box for this. Redis没有任何开箱即用的功能。 You can create a secondary index in Redis on the value. 您可以在Redis上创建值的二级索引 It comes at a cost though - you need more memory to store the index. 它需要付出代价 - 你需要更多的内存来存储索引。

you can build your own 'value index'. 你可以建立自己的'价值指数'。

for example, you can add a second key with type sorted set, with key: A, value: 1(1), 2(2), 3(3), 4(4), the number in parentheses is score, you can use your own score like timestamp. 例如,你可以添加第二个键类型为sort的键,其中键:A,值:1(1),2(2),3(3),4(4),括号中的数字是得分,你可以使用自己的分数,如时间戳。

so when you want first ten primary key with value A, use this: 所以当你想要前十个主键值为A时,请使用:

zrangebyscore A -inf +inf limit 0 10 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM