4

后端用什么方法实现限制用户访问资源数量比较好?

 2 years ago
source link: https://www.v2ex.com/t/836509
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  程序员

后端用什么方法实现限制用户访问资源数量比较好?

  LeeReamond · 1 天前 · 898 次点击

需求:数据库内共有 10 万个资源,限制每用户每小时最多访问其中 100 个。

目前的思路:redis 里针对每个用户生成一个数组储存该用户已访问过的资源的 id ,每次请求时判断 ID 是否在数组中。

另外因为需求是限制访问资源种类而不是资源数量(比如实际上允许用户多次请求同一资源,只要不是不同资源就不会加以限制),且访问是与用户挂钩,所以似乎不太适合在业务前实现。

1 、感觉这种方式效率比较低,不是很优雅。

2 、限制有 1 小时时限,如何在数据超过期限后自动从数组中清除也是没啥太好办法。

这种需求有什么通用方案吗?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK