首页 > php > 如何设置API使用限制

如何设置API使用限制 (How to set API usage limits)

2014-05-23 phpapirest

问题

我正在创建一个私有RESTful API,供我的移动应用程序使用。我想根据用户设置使用限制(客户端必须注册才能使用该应用程序)。我对如何实现这一点很困惑。我是否针对每个用户存储API调用计数,在每个请求上获取并检查它,并在请求成功执行后更新它?随着用户开始增长,它不会在服务器上产生非凡的开销吗?有没有更快的方法来实现这一目标?

解决方法

您必须记录每个用户的使用情况,如果您正在为每个API方法执行操作,那么这将为每个用户创建指数数据使用,您应该在选择数据存储方法时考虑这一点。

我可能会考虑使用key => value存储方法,比如redis,并将这些数据远离主数据存储,这样你的节流验证不会堵塞主数据服务器。

问题

I'm creating a private RESTful API that is to be consumed by my mobile application. I want to set usage limit to the functions on the basis of users (a client has to register to use the app). I'm confused as to how to achieve that. Do I store API call count against every user, fetch and check it on every request and update it once a request is executed successfully? Wouldn't it create extraordinary overhead on the server as users start to grow? Is there any faster way to achieve that?

解决方法

You will have to record usage against each user, if you are doing for every API method, then this will create exponential data usage per user, and you should consider this when choosing your data-storage method.

I would probably consider a key=>value storage method for this, like redis, and keep this data away from your main data store, so that your throttle validation isn't clogging up the main data server.

相似信息