如何在 Java 的会话对象中强制执行每次发布 5 分钟的规则?
我正在尝试找出如何强制执行每个帖子/操作 5 分钟的规则。
我正在使用 Java 中的 Wicket 开发一个 Web 应用程序,并且我有一个会话类,我计划使用它来跟踪每个用户的这些计时器。我不打算将时间戳存储在数据库中。
我的会话类中有下面的方法,现在我试图弄清楚如何存储和检查用户上次发布的时间与当前时间之间是否有 5 分钟的差异。
如果,比如说 java.util.Date,适合于此,我如何在几分钟内获得其中两个之间的差异。
public boolean isAllowedToPost() {
if(null OR has 5 minutes passed since last post) {
// set the new timestamp
return true;
}
else
{
return false;
}
}
I'm trying to figure out how to enforce a 5 minute per post/action rule.
I'm developing a web application with Wicket in Java and I've got a session class which I was planning on using to keep track of these timers on a per-user basis. I wasn't planning on storing the timestamp in a database.
I've got the method below in my session class, and now I'm trying to figure out how to go about storing and checking that there's a 5 minute difference between the last time the user posted and the current time.
If, lets say java.util.Date, would be suitable for this, how do I get the difference between two of these, in minutes.
public boolean isAllowedToPost() {
if(null OR has 5 minutes passed since last post) {
// set the new timestamp
return true;
}
else
{
return false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最可靠的方法是将时间戳存储在数据库中,因为客户端可以简单地注销/登录或废弃会话(删除 cookie 或重新启动网络浏览器)以获得新会话。
如果您坚持以任何方式将其存储在会话中,那么其中一种方法是在
post()
期间设置时间戳,并将其用作isAllowedToPost()
中的比较材料。您可以通过System#currentTimeMillis()
。不需要java.util.Date
。启动示例(假设整个 bean 属于会话范围):
The most robust way would be storing the timestamp in a database, since the client could simply logout/login or trash the session (delete cookie or restart webbrowser) to obtain a new session.
If you insist in storing it in session at any way, then one of the ways would be setting the timestamp during
post()
and use it as comparision material inisAllowedToPost()
. You can obtain the current timestamp bySystem#currentTimeMillis()
. No need forjava.util.Date
.Kickoff example (assuming that this whole bean is session scoped):