Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

spring

jsf-2

Im developing an application based on Spring and JSF 2.0. There is a requirement to remember how many times client from specific ip address tried to submit form during last 3 minutes. If during 3 minutes more than 3 submit attempts were recorded then captcha should be displayed.

Im thinking about using ConcurrentMap<String, ConcurrentLinkedQueue<Long>> for storing ip address (String) and submit time (milis) in queue (ConcurrentLinkedQueue<Long>). The queue will be cleaned by Quartz in 3-min intervals (milis older than 3-min will be removed). To check if display captcha I will check if queue size > 3.

Is this correct approach? Do you have any better ideas?

like image 696
michal777 Avatar asked Jan 16 '23 23:01

michal777


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 ones, and it is not as wasteful as a String in terms of the memory that it uses, which might become important in very high-volume situations.

like image 173
Sergey Kalinichenko Avatar answered Jan 30 '23 13:01

Sergey Kalinichenko


Personally, I store my IP's as Longs rather than Strings.

You will see a performance improvement.

like image 20
KingCronus Avatar answered Jan 30 '23 12:01

KingCronus