What is the best way to store ip addresses in an application?

Im developing an application based on Spring and JSF 2.0. You must remember how many times a client from a specific ip address tried to send a form in the last 3 minutes. If more than 3 sending attempts were recorded within 3 minutes, then captcha is displayed.

I am thinking of using ConcurrentMap<String, ConcurrentLinkedQueue<Long>> to store the ip address ( String ) and send the time (milis) to the queue ( ConcurrentLinkedQueue<Long> ). The queue will be cleared with quartz at 3-minute intervals (milis older than 3 minutes will be deleted). To check if the conversion will be displayed, I will check if the queue size is> 3.

Is this the right approach? Do you have any ideas?

+4
source share
2 answers

Java provides a special class for storing IP addresses: java.net.InetAddress . Unlike Long , it is capable of handling 128-bit addresses in addition to 32-bit addresses, and it is not as wasteful as String in terms of used memory, which can become important in a very high level.

+7
source

Personally, I save my IP as Longs, not Strings.

You will see a performance improvement.

+2
source

All Articles