SagyDrucker
SagyDrucker

Reputation: 21

redis store 128 bit number

I want to efficiently search IPv6 subnet range using redis.

i thought of storing the IPv6 numeric addresses in redis and search them by range. those are 128-bit ints, e.g:

import ipaddress
int(ipaddress.ip_address(u'113f:a:2:3:4:1::77'))

> 22923991422715307029586104612626104439L

and query by range:

ZRANGEBYSCORE numerics <subnet-S-start> <subnet-S-end>

HOWEVER, redis sorted-sets can hold score of up to 2^53, so all my large ints are being trimmed and I'm losing precision.

Is there a way to save such large numbers in redis without losing precision?

Do you have a better suggestion? Thanks

Upvotes: 1

Views: 1453

Answers (1)

Not_a_Golfer
Not_a_Golfer

Reputation: 49205

You can use the lexical range API, it will suit you exactly. https://redis.io/commands/zrangebylex

Insert the addresses with a score of 0, I don't even think you need to encode them as numbers, just pad the individual bytes, and you should be able to query an range.

Upvotes: 6

Related Questions