使用 Redis 排序集中的部分键名查找值 [英] Finding values by using partial key name in a Redis Sorted Set

查看:25
本文介绍了使用 Redis 排序集中的部分键名查找值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个排序集,它具有以下键名和值:

I have a sorted set which has the following key name and values :

zrange bargraph:branch:1:category:2:product:4
1) "76"
2) "55"
3) "10"
4) "84"

redis 中是否有一种机制,我可以在使用 zrange 时使用通配符(也许是 ?)来获得相同的值?换句话说,redis 是否支持 zrange bargraph:branch:1:category:2?如果没有,如果我只知道类别 id(2) 而不是产品 id(4) 而不使用不同的排序集,我怎样才能最好地获得所有值?

Is there a mechanism in redis where I can use a wildcard character(maybe an ?) when using zrange to still get the same values? In other words, does redis support zrange bargraph:branch:1:category:2? If not, how best can I get all the values if I only know the category id(2) and not the product id(4) without using a different sorted set?

推荐答案

正如您所提到的,KEYS 效率低下,因为引擎对键执行线性扫描.不幸的是,没有您正在寻找的通配符解决方案

As you've mentioned, KEYS is inefficient because the engine performs a linear scan for keys. Unfortunately, there is no wildcard solution such as you are looking for

考虑为每个类别的产品密钥使用 SET:

Consider using a SET for your product keys per category:

SADD bargraph:branch:1:category:2 1 2 3 4

要获取所有设置的类别成员,请执行以下操作:

to fetch all set category members do:

SMEMBERS bargraph:branch:1:category:2

如果你不关心对你的分数求和,或者每个排序集有不同的项目,你可以像这样对你的每个产品排序集进行联合:

If you don't care about summing your scores, or have different items per a sorted set, you can do a union of your per product sorted sets like this:

ZUNIONSTORE bargraph:branch:1:category:2:product:all 4 bargraph:branch:1:category:2:product1 bargraph:branch:1:category:2:product2 bargraph:branch:1:category:2:product3 bargraph:branch:1:category:2:product4

现在你可以zrange bargraph:branch:1:category:2:product:all

您将上述操作流水线化以获得更好的性能

You pipeline the above operations for better performance

这篇关于使用 Redis 排序集中的部分键名查找值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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