允许用户每 5 分钟最多发送 20 条评论?

allow user to send max 20 comments in each 5 minutes?

我想做一个评论系统,每5分钟只允许用户发送大约20条评论。这样我可以确定没有人会发送太多垃圾邮件...

我想我可以使用 sharedpreferences 计算评论数。每条评论我都会阅读并增加数量。但是最后 5 分钟呢?

int value = sharedPref.getInt("count", 0);
value = value + 1;
editor.putInt("count", value);

需要对 Now 和 Now 进行 date/time 差异运算 - 5 分钟。

SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd:HH:mm");
String cTime = sdf.format(new Date());

Date date = formatter.parse(cTime);
Calendar futureTime = Calendar.getInstance();
futureTime.setTime(date);
futureTime.add(Calendar.MINUTES, 5); //This will be now + 5 minutes.

然后您可以重置 SharedPreferences 变量。

将用户、时间戳和计数保存在您的 SharedPreference variable/data 中的密钥对中。您可能必须创建一个具有两个或多个这些值的自定义对象,因为它是自己的自定义 class。可能User就是字符串,另外两个在value里面,后面可以解析出来。使用 HashMap:

Map<String, String> aMap = new HashMap<String, String>();

人们在 game/app 世界中一直这样做。你每小时得到 100 个硬币,等等。我已经在 c#/Unity3D 中为 iOS/Android 实现了一些东西。您每小时免费获得 100 个金币,如果需要,您可以通过应用内购买获得更多金币,或者让时间过得更快!