store ip ranges in Redis

后端 未结 4 1897
失恋的感觉
失恋的感觉 2021-02-02 02:22

I have a lot of IP ranges of different providers. For example

P1: 192.168.1.10 - 192.168.1.50, 192.168.2.16 - 192.168.2.49,
P2: 17.36.15.34 - 17.36.15.255,
P3: .         


        
4条回答
  •  南笙
    南笙 (楼主)
    2021-02-02 03:14

    In my opinion best solution would be sorted set.

    To insert range use ZADD.
    To member assign range_name.
    To score assign highest value in range.

    ZADD ip_table 3232235826 some_name
    

    Then for finding range use ZRANGEBYSCORE with user_ip as min_value and limit = 1.

    ZRANGEBYSCORE ip_table user_ip +inf LIMIT 0 1
    

    It will find range with smallest ip at endpoint that is greater than or equal to user_ip.

提交回复
热议问题