[英]What is the best way to store ip addresses in application?
Im developing an application based on Spring and JSF 2.0. 我正在开发基于Spring和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.
需要记住特定IP地址的客户端在过去3分钟内尝试提交表单的次数。 If during 3 minutes more than 3 submit attempts were recorded then captcha should be displayed.
如果在3分钟内记录了超过3次提交尝试,则应显示验证码。
Im thinking about using ConcurrentMap<String, ConcurrentLinkedQueue<Long>>
for storing ip address ( String
) and submit time (milis) in queue ( ConcurrentLinkedQueue<Long>
). 我正在考虑使用
ConcurrentMap<String, ConcurrentLinkedQueue<Long>>
来存储ip地址( String
)并在队列中提交时间(milis)( ConcurrentLinkedQueue<Long>
)。 The queue will be cleaned by Quartz in 3-min intervals (milis older than 3-min will be removed). Quartz将以3分钟的间隔清理队列(将删除3分钟以上的milis)。 To check if display captcha I will check if queue size > 3.
要检查是否显示验证码,我将检查队列大小是否> 3。
Is this correct approach? 这是正确的方法吗? Do you have any better ideas?
你有更好的想法吗?
Java provides a special class for storing IP addresses: java.net.InetAddress
. Java提供了一个用于存储IP地址的特殊类:
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. 与
Long
不同,除了32位地址之外,它还能够处理128位地址,并且就其使用的内存而言,它并不像String
那样浪费,这在非常大量的情况下可能变得很重要。
Personally, I store my IP's as Longs rather than Strings. 就个人而言,我将我的IP存储为Longs而不是Strings。
You will see a performance improvement. 您将看到性能提升。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.