Redis:排序并获得n个邻居键 [英] Redis: Sort and get n Neighbour Keys

查看:48
本文介绍了Redis:排序并获得n个邻居键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在 Redis 中有这些对:

Let's say I have these pairs in Redis:

1237.56 "John"
1224.59 "Robert"
1213.34 "Mahmoud"
1242.90 "George"
1020.11 "Mary"
1723.09 "Jay"
1589.77 "Khan"
1106.62 "Albert"

键总是一个十进制数.当提供一个数字时,我想在两个方向上用最接近的数字获得两个键.

The keys are always a decimal number. When provided a number, I would like to get two keys in both directions with closest number.

例如,如果我搜索1242的邻居,结果集应该有:

For example, if I search for the neighbours of 1242, the resultset should have:

1224.59 "Robert"
1237.56 "John"
1242.90 "George"
1589.77 "Khan"

这可能吗?我还是 Redis 的新手,感谢您的帮助.

Is this possible? I'm still new to Redis and thanks for any help.

推荐答案

是和不是同时.

为什么是? 唯一一种 redis 数据类型 - SORTED SET 允许您获取基于十进制的值范围.因此,使用 ZRANGEBYSCORE,您可以按分数范围获取值.

Why yes? The only one redis data type - SORTED SET allow you to get range of values based on decimal. So with ZRANGEBYSCORE you can get values by score range.

ZRANGEBYSCORE theKey 1220 1600

为您提供所需的数据集.

Gives you sought-for data set.

为什么不?在您的问题中,您询问分数的邻居.ZRANGEBYSCORE 可以获取从最小分数到最大分数的值,但不能获得小于给定的 X 值"或大于给定的 X 值".

Why No? In your question your ask about neighbours of score. ZRANGEBYSCORE can get values from min score to max score but not the "get X values less then given" or "X values great then given".

这篇关于Redis:排序并获得n个邻居键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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