如何正确设计一个宁静的API以使缓存无效? [英] How to proper design a restful API to invalidate a cache?

查看:62
本文介绍了如何正确设计一个宁静的API以使缓存无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要Service2数据的应用程序,它将永远为给定请求返回相同的答案,除非更新了其后备数据库。数据库很少进行更新,比如说每年更新两次。

I have an Application which requires data from Service2, which will return the same answer for a given request, forever, unless its backing database is updated. The database is updated very rarely, let's say twice per year.

我想设计一个解决方案,以便应用程序缓存Service2的答案,但从外部提供一个功能,以使应用程序的缓存无效。我本来想从应用程序中公开一个RESTful Web服务,但是我对如何正确设计它感到困惑。

I would like to design a solution so that the Application caches the answers from Service2, but to externally provide a feature so as to invalidate the cache of Application. I thought of exposing a RESTful webservice from the Application, but I am confused on how to design it correctly.

/ application / cache / invalidate 是非RESTful URL-我正在考虑使用HTTP POST调用 / application / cache / 。但是,在我看来,对于适当的RESTful设计,当使用POST更新资源时,要更新的内容应包含在请求的正文中。

/application/cache/invalidate is a non RESTful URL - I was thinking about /application/cache/ to be called with HTTP POST. However, it looks to me that for a proper RESTful design, when POST is used to update a resource, the content to update should be contained in the body of the request.

设计 InvalidateCache静态Web服务的正确方法是什么?

What is the right way to design a "InvalidateCache" restful webservice?

推荐答案

请考虑使用 DELETE 而不是 POST 和url:

Consider using DELETE instead of POST and for the url:

/application/cache/ 

在REST中,两个 PUT DELETE 被认为是独立动作。即,它们可以以相同的结果资源状态重复多次。在这种情况下,您的缓存就是资源,并且多个 DELETE 将导致相同的状态,即已清除的缓存。

In REST, both PUT and DELETEare considered to be indempotent actions. That is, they can be repeated multiple times with the same resulting resource state. In this case, your cache is the resource and multiple DELETE's will result in the same state, a cleared cache.

您可以考虑在网址中添加一个描述符,以澄清您正在清除缓存的内容而不是删除缓存对象本身。

You could consider adding a descriptor to your url to clarify that you are clearing the contents of your cache and not deleting the cache object itself. Something like

/application/cache/contents

,但这取决于您。走那条路线还可能使您有必要在需要时有选择地从缓存中删除。

perhaps, but that is up to you. Going that route could also potentially let you selectively delete from your cache if necessary.

这篇关于如何正确设计一个宁静的API以使缓存无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆