如何处理 RESTful 调用中的浏览量?
我希望请求返回一些“分析”作为请求的一部分:最重要的是,我希望每个 GET 请求返回 GET 请求被调用的次数。
这与 RESTful 概念根本不相容吗?
如果它不是根本上不兼容,我怎样才能让我的 RESTful 服务器为每个 GET 请求返回不同的结果,根据这个问题的定义,每个 GET 请求意味着下一个 GET 请求必须返回不同的结果?
如果从根本上不可能以 RESTful 方式完成,我应该放弃 REST 还是完全放弃 GET?
PS:这是我关于 SO 的第一个问题,所以显然我无法发表评论,除非我达到 15 名代表,所以如果评论者/回答者可以给我投票,这样我就可以成为 SO 社区的一部分,那就太好了: )
I'd like to have a request returning some "analytics" as part of the request: most importantly, I'd like every GET requests to return the number of times the GET request has been called.
Is this fundamentally incompatible with the RESTful concept?
If it's not fundamentally incompatible, how can I have my RESTful server return a different result for each GET request seen that, by definition of this problem, every GET requests means the next GET request must return something different?
If it's fundamentally impossible to do in a RESTful way, should I drop REST or do I drop GET altogether?
P.S: This is my first question on SO so apparently I won't be able to comment unless I hit 15 rep so it would be nice if commenters/answerers could upvote me so that I can be part of the SO community : )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里原则上不存在与REST不兼容的情况。您应该考虑不断变化的资源不会从缓存中受益(假设您不希望访问计数变得陈旧)。您还应该考虑该数字始终“最终一致”;也就是说,如果您有很多并行执行,它将代表请求的某些子集的计数。您还应该考虑在“X-Visit-Count”标头而不是主要负载中返回计数,以使该功能更加通用,并避免污染您返回的任何负载,并可能允许更好的缓存。但是,每次获取资源时,资源都不会返回不同的表示形式,这与 REST 完全不同。
There is no incompatibility with REST in principle here. You should consider the effect that a constantly-changing resource will not benefit from caching (assuming you don't want the visit count to go stale). You should also consider that number to be constantly "eventually consistent"; that is, it will represent the count for some subset of the requests if you have lots executing in parallel. And you should also consider returning the count in an "X-Visit-Count" header instead of the main payload to make the feature more generic and avoid polluting whatever payload you do return, and potentially allow for better caching. But there's nothing at all "against REST" for a resource to return a different representation each time you GET it.