简体   繁体   English

如何使用数据库记录请求数,将最大网站请求数限制为每天每用户10个?

[英]How do I limit max website requests to 10 per user per day, using a database to record request counts?

I have a person's username, and he is allowed ten requests per day. 我有一个人的用户名,每天可以允许他十个请求。 Every day the requests go back to 0,and old data is not needed. 每天,请求都回到0,不需要旧数据。

What is the best way to do this? 做这个的最好方式是什么?

This is the way that comes to mind, but I am not sure if it's the best way 这就是我想到的方法,但是我不确定这是否是最好的方法
(two fields, today_date, request_count): (两个字段,today_date,request_count):

  1. Query the DB for the date of last request and request count. 在数据库中查询最后一个请求的日期和请求计数。
  2. Get result and check if it was today. 获取结果并检查是否是今天。
  3. If today, check the request count, if less than 10, update query database to ++count . 如果是今天,请检查请求计数,如果少于10,则将查询数据库更新为++count
  4. If not today, update the DB with today's date and count = 1 . 如果不是今天,请使用今天的日期和count = 1更新数据库。

Is there another way with fewer DB queries? 还有另一种方法来减少数据库查询吗?

I think your solution is good. 我认为您的解决方案很好。 It is possible to reset the count on a daily basis too. 也可以每天重置计数。 That will allow you to skip a column, but you do need to run a cron job. 这样可以跳过一列,但是您确实需要运行cron作业。 If there are many users that won't have any requests at all, it is needless to reset their count each day. 如果有很多用户根本没有任何请求,则无需每天重置其计数。

But whichever you pick, both solutions are very similar in performance, data size and development time/complexity. 但是无论您选择哪种方式,两种解决方案在性能,数据大小和开发时间/复杂性上都非常相似。

Just one column request_count . 仅一列request_count Then query this column and update it. 然后查询此列并进行更新。 As far as I know with stored procedures this may be possible in one single query. 据我所知,使用存储过程可以在一个查询中实现。 Even if not, it will be just two. 即使没有,也只有两个。 Then create a cron job, that calls a script, that resets the column to 0 every day at 00:00. 然后创建一个cron作业,该作业调用一个脚本,该脚本将每天00:00将该列重置为0

To spare you some requests to the DB define 为了节省您对数据库定义的一些请求

  1. the maximum number of requests per day allowed. 每天允许的最大请求数。
  2. the first day available to your application (date offset). 您的应用程序可用的第一天(日期偏移量)。

Then add a requestcount field to the database per user. 然后将每个用户的requestcount字段添加到数据库。

On the first request get the count from the db. 在第一个请求上,从数据库获取计数。

The count is always the number of the day multiplied with the maximum + 1 of requests per day plus the actual requests by that user: 该计数始终是天数乘以每天的最大请求数+ 1,再加上该用户的实际请求数:

   day * (max + 1) + n

So if on first request the count from the db is actually higher than allowed, block. 因此,如果在第一次请求时,来自数据库的计数实际上高于允许的值,则阻塞。

Otherwise if it's lower than the current day base, reset to the current day base (in the PHP variable) 否则,如果它低于当前日基,则重置为当前日基(在PHP变量中)

And count up. 并数。 Store this value into the DB. 将此值存储到数据库中。

This is one read operation, and in case the request is still valid, one write operation to the DB per request. 这是一次读操作,如果请求仍然有效,则每个请求对DB进行一次写操作。

There is no need to run a cron job to clean this up. 无需运行cron作业即可清理此问题。

That's actually the same as you propose in your question, but the day information is part of the counter value. 这实际上与您在问题中提出的建议相同,但是日期信息是计数器值的一部分。 So you can do more with one value at once, while counting up with +1 per request still works for the block. 因此,您可以一次使用一个值执行更多操作,而每个请求的+1计数仍适用于该块。

You have to take into account that each user may be in a different time zone than your server, so you can't just store the count or the "day * max" trick. 您必须考虑到每个用户所处的时区可能与服务器所在的时区不同,因此您不能仅存储计数或“天*最大”技巧。 Try to get the time offset and then the start of the user's day can be stored in your "quotas" database. 尝试获取时间偏移,然后可以将用户日的开始时间存储在“配额”数据库中。 In mySQL, that would look like: 在mySQL中,它看起来像:

`start_day`=ADDTIME(CURDATE()+INTERVAL 0 SECOND,'$offsetClientServer')

Then simply look at this time the next time you check the quota. 然后,下次查看配额时,只需查看该时间即可。 The quota check can all be done in one query. 配额检查都可以在一个查询中完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM