简体   繁体   English

检查Redis中的多个键

[英]Checking multiple keys in redis

So here's an example of what the keys I have set in redis: 所以这是我在redis中设置的键的示例:

SADD group:test subgroup:1
SADD subgroup:1 user:1
SADD subgroup:1 user:2
SADD group:test subgroup:2
SADD subgroup:2 user:3

So what I want to achieve is that I want to find out which subgroup has space in the key group:test and return that. 所以我想要实现的是我想找出哪个子组在key group:test有空间并返回。 Let's say for example that the maximum users per sub group is 2. 例如,假设每个子组的最大用户数为2。

In that case, subgroup 2 would have space so it should return subgroup:2 . 在这种情况下,子组2将有空间,因此它应该返回subgroup:2

If there isn't any space I'd like it to return the last subgroup +1. 如果没有任何空间,我希望它返回最后一个子组+1。 So in that case it would return subgroup:3 . 因此,在这种情况下,它将返回subgroup:3

Currently, the way this is written means that I have to use smembers to get the members of the group:test set and loop through each key using scard to see which sub group has space. 当前,这种编写方式意味着我必须使用smembers来获取group:test集合的成员,并使用scard遍历每个键以查看哪个子群组具有空间。 If the loop ends and there's no space in any key, then I just add 1 to the last subgroup and use that. 如果循环结束并且任何键中都没有空格,那么我只需在最后一个子组中添加1并使用它。

Users can leave a subgroup after they have joined. 用户加入后可以离开子组。 I used multi to do this, but it doesn't seem very efficient as it continues running scard even after it has found space in a key. 我使用multi来做到这一点,但是它看起来效率不高,因为即使在键中找到了空间,它仍会继续运行。 So let's say in this example user:2 left subgroup:1 , it would still check subgroup:2 to see if it had space. 因此,在这个示例中,假设user:2离开了subgroup:1 ,它仍然会检查subgroup:2来查看是否有空间。

I'm more than willing to restructure the data if there's a way to make this work. 如果有办法可以完成这项工作,我非常愿意重组数据。 The only thing I don't want to change is that the key for users needs to be stored within the subgroup set. 我唯一不想更改的是,用户密钥必须存储在子组集中。

If there's a way to do this with a LUA script, I'd also be willing to do that. 如果有办法使用LUA脚本执行此操作,我也很愿意这样做。

I'm really looking for an efficient way to do this. 我真的在寻找一种有效的方法来做到这一点。

Can anyone help with this? 有人能帮忙吗?

A Lua script isn't the way here - rewording the above requirement: we're looking for an efficient way to get the cardinality of Sets (eg subgroup:1 ) at any instant, and we're specifically interested in Set the one with the least members ("has space"). Lua脚本不是这里的方法-重新表达上述要求:我们正在寻找一种随时获取Sets基数(例如subgroup:1 )的有效方法,并且我们特别感兴趣的是Set with最少的成员(“有空间”)。

To do that, you keep another key in your database - lets call it counts . 为此,您需要在数据库中保留另一个键-称之为counts That key holds a Sorted Set data structure, where each element is the name of one of your sets and the score is the its current cardinality. 该键具有“排序集”数据结构,其中每个元素是其中一个集的名称,而得分是其当前基数。

By "current" I mean that each SADD , SREM and DEL that your code performs on subgroups, needs to be coupled with the relevant ZADD on the counts key. “当前”是指您的代码在子组上执行的每个SADDSREMDEL都需要与counts键上的相关ZADD耦合。

When you have all that in place, all that you need to do is query the key for the lowest-scored member: 完成所有操作后,您需要做的就是查询得分最低的成员的密钥:

ZRANGE counts 0 0

The cost in maintaining this extra structure is the complexity of updates whenever subgroup changes. 维护此额外结构的成本是每当子组发生更改时更新的复杂性。 Sorted Sets' operations are typically O(log(N)) so they're pretty efficient and wouldn't burden the server too much. 排序集的操作通常为O(log(N)),因此它们非常高效,不会给服务器带来太多负担。

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

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