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

查看:17
本文介绍了如何正确设计一个宁静的 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 网络服务,但我对如何正确设计它感到困惑.

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"宁静网络服务的正确方法是什么?

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 中,PUTDELETE 都被认为是幂等操作.也就是说,它们可以以相同的结果资源状态重复多次.在这种情况下,您的缓存是资源,多个 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.

您可以考虑在您的 url 中添加一个描述符,以说明您正在清除缓存的内容而不是删除缓存对象本身.类似的东西

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天全站免登陆