To support looking up IP addresses, we added integer scores to a ZSET to represent the
beginning of IP address ranges for a given city ID. In order to find a city given an IP
address, we map the IP address to a similarly calculated score and then find the city ID
that has the largest starting IP address less than or equal to the IP address we pass. We
can use ZREVRANGEBYSCORE with the optional START and NUM arguments set to 0 and 1,
respectively, to fetch this information. After we’ve discovered the city ID, we can fetch
the city information from our HASH. Our function for finding which city an IP address
is in can be seen next.
We can now look up city information based on IP address and begin to analyze where
our users are coming from. This method of converting data into integers for use with
a ZSET is useful, and can greatly simplify the discovery of individual items or ranges.
We’ll talk more about these kinds of data transformations in chapter 7. But for now, let’s
look at how we can use Redis to help us find and connect to other servers and services.
TRY REDIS ENTERPRISE CLOUD FREE
Redis Enterprise Cloud provides complete automation of day-to-day database operations. Start now with 30MB of free storage.
© 2021 Redis Labs. Redis and the cube logo are registered trademarks of Redis Labs Ltd.