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?
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.
Personally, I store my IP's as Longs rather than Strings.
You will see a performance improvement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With