通过Redis排序集进行反向分页 [英] Reverse Pagination Through A Redis Sorted Set

查看:621
本文介绍了通过Redis排序集进行反向分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑具有以下成员的Redis排序集:

Consider a Redis sorted set with the following members:

ZADD mySortedSet 11 "A"
ZADD mySortedSet 21 "B"
ZADD mySortedSet 32 "C"
ZADD mySortedSet 46 "D"
ZADD mySortedSet 53 "E"
ZADD mySortedSet 68 "F"
ZADD mySortedSet 72 "G"
ZADD mySortedSet 82 "H" 
ZADD mySortedSet 94 "I"
ZADD mySortedSet 104 "J"
ZADD mySortedSet 113 "K"

如果我想从任意切片开始以相反的顺序进行分页,则可以从以下内容开始:

If I want to do pagination in reverse order, starting at an arbitrary slice, I can start with this:

// Returns G, F, E, as expected.
ZREVRANGEBYSCORE mySortedSet 72 (46

现在,仅知道我的上限是46,就可以通过以下操作获得集合D,C和B中的前3个项目,而无需知道下限:

Now, knowing only that my upper bound is 46, I can get the previous 3 items in the set, D, C, and B, without knowing the lower bound by doing:

ZREVRANGEBYSCORE mySortedSet 46 -inf LIMIT 0, 3

我的问题是,仅知道上限为72,如何才能按顺序获得集合中的下三个项目J,I和H?

My question is, how can I get the next 3 items in the set, J, I and H, in that order, knowing only that the upper bound is 72?

// Good start, returns K, J, I, H
ZREVRANGEBYSCORE mySortedSet +inf (72

// Returns K, J, I, due to the offset of 0.  I don't know what the correct offset is because it's from the start of the range, not the end.
ZREVRANGEBYSCORE mySortedSet +inf (72 LIMIT 0, 3

我想我想要的是负偏移量,我不认为这是受支持的.

What I think I want is a negative offset, which I don't think is supported.

// Would return J, I, H, but actually returns an empty set.
ZREVRANGEBYSCORE mySortedSet +inf (72 LIMIT -1, 3

我可以在一定范围内伪造它,然后反转这些项目,但是我正在寻找本机Redis解决方案(如果存在).

I can fake it with a forward range, and then reversing those items, but I'm looking for a native Redis solution, if one exists.

// Returns H, I, J - the items I want, but reversed.
ZRANGEBYSCORE mySortedSet (72 +inf LIMIT 0, 3

有什么想法吗?

要清楚,我知道有ZRANGE和ZREVRANGE,但是在此查询配置文件中,我不知道实际的索引,只是分数.

To be clear, I know there is ZRANGE and ZREVRANGE, but in this querying profile, I won't know the actual index, just the score.

推荐答案

获取元素的排名,然后按索引进行操作很简单.假定对您的应用程序可用的唯一输入是初始分数范围72和46,则可以执行以下操作:

It's trivial to get the rank for an element, and then work by indexes. Presuming that the only inputs available to your application are the initial score bounds of 72 and 46, you can do this:

redis 127.0.0.1:6379> ZREVRANGEBYSCORE mySortedSet 72 (46
1) "G"
2) "F"
3) "E"
redis 127.0.0.1:6379> ZREVRANK mySortedSet G
(integer) 4
redis 127.0.0.1:6379> ZREVRANGE mySortedSet 1 3
1) "J"
2) "I"
3) "H"
redis 127.0.0.1:6379> 

唯一的附加调用是O(log(N))ZREVRANK调用.从那里开始,有一些客户端数学方法可以获取您感兴趣的范围的新索引,而ZREVRANGE则可以获取所需的值.

The only additional call is the O(log(N)) ZREVRANK call. From there, it's a bit of client side math to get the new indexes for the range you're interested in, and ZREVRANGE to get the values you want.

我在Redis 2.6rc5上进行了测试,但是它可以在2.0以上的任何版本上使用.

I tested this on Redis 2.6rc5, but it should work on any version over 2.0.

这篇关于通过Redis排序集进行反向分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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