I need to store IP addresses in a redis hash.
Will there be considerable memory savings if the IP is stored as an integer instead of a string?
I would be using Ruby’s IPAddr to convert the IP to an int.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It depends on how you do it. In Redis keys and (leaf) values are strings. If you would convert an IP address to an int and send it to Redis like the following code you wouldn’t save much:
The IP “255.255.255.255”, for example, is 15 bytes in dotted quad form, its integer representation “4294967295” is ten bytes when saved as a string, which is what the code above will do.
To get down to just four bytes stored in Redis you would have to send the raw bytes “\xFF\xFF\xFF\xFF”.
In Ruby you would do it this way:
And then when you read it back
What
IPAddr.htonandIPAddr.ntopdo is this:Then there’s the whole thing about IPv6 and whatnot, but I think
IPAddrhas you covered there.