Csv с ip адресами камер

Csv с ip адресами камерUsing Redis for this is a great idea.

Assuming you are handling IPV4 addresses only, you could use the three first parts of the IP address. It doesn’t look like the last part is significant when looking for the country.

I think you could store all of the data in a unique redis sorted set. The values will be the name of countries. And the score the end IP address, converted to decimal.

By example:

Register this in a Redis Sorted Set named ‘countries':

Nb: I concatenate the score with the country name just to ensure the stored value is unique. It will allow to register more than one IP range for each country.

1.0.7 ==> score = 65543 for Australia

1.0.15 ==> score = 65551 for China

And so on.

When you want to check a given IP address, apply the same algorithm. By example if you want to find the country for, you compute a score of

Now you make a request to Redis:

It will return you the first value, with a score at least equal to 65541. It’s the name of the country (of course you’ll have to drop the last part of the returned string).

Csv с ip адресами камерThe performance of the search will depend on the number of items in the sorted set. If N is this number, the time complexity of the search will be O(Log(N)).

I have no idea of the number of items in your set (that it to say the number of IP ranges), but if you have any performance problem, you can split data in multiple sorted sets.

Use the first part of the IP address as a part of the sorted set key (countries:1 stores the data for IP address from to, countries:2 stores the data for IP address from to, etc).

Then use the same principle as above but with a score computed from the second and third parts of the end IP address, and look in the sorted set corresponding to the first part.

Комментирование и размещение ссылок запрещено.

Thanx: МГУДТ