Redis application case to find the range of a value)

Source: Internet
Author: User
Tags ranges

This article comes fromRedisIn a Google group issue, a student posted a post asking for help, saying that the following problem should be solved: He has a list of addresses corresponding to the IP range, now you need to give an IP address, quickly find the IP address range, that is, to determine all the locations of the IP address. This question was answered by redis author Salvatore sanfilippo (@ antirez. The answer is as follows:

For example, there are two ranges: 10-20 and 30-40.

    • A_start 10, a_end 20
    • B _start 30, B _end 40

The starting position of the two ranges is stored in the sorted sets data structure of redis. The starting value of the basic range is used as the score, and the range name is added with start and end as the value:

 
Redis 127.0.0.1: 6379> zadd ranges 10 a_start (integer) 1 redis 127.0.0.1: 6379> zadd ranges 20 a_end (integer) 1 redis 127.0.0.1: 6379> zadd ranges 30 B _start (integer) 1 redis 127.0.0.1: 6379> zadd ranges 40 B _end (integer) 1

In this way, after the sorted sets are inserted, the starting positions are arranged in order.

Now I need to find out the range of the Value 15. I only need to search for the following zrangbyscore:

 
Redis 127.0.0.1: 6379> zrangebyscore ranges (15 + INF limit 0 11) "a_end"

This command is used to find the first value greater than 15 in sorted sets. (+ INF indicates positive infinity in redis, and brackets above 15 indicate> 15 rather than> = 15)

The search result is a_end. Because all values are arranged in order, we can determine that 15 is in the range of a_start to a_end, that is, 15 is in the range of. This is a success.

Of course, if you find a start, such as 25, execute the following command

 
Redis 127.0.0.1: 6379> zrangebyscore ranges (25 + INF limit 0 11) "B _start"

The returned result indicates that the next node is a Start Node. That is to say, the value 25 is not between any start and end and does not belong to any range.

Of course, this example is only applicable to cases similar to the above IP Range lookup, because these values do not overlap. If there is overlap, the problem itself becomes a one-to-many problem. Well, if there is a overlap, how can we solve it? Welcome to the challenge.

If you have more interestingApplicationScenario. You are welcome to submit your contribution to us.

Related address: groups.google.com

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.