redis store 128 bit number

半腔热情 提交于 2019-12-12 04:19:09

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/45133259/redis-store-128-bit-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!