简体   繁体   English

REST-删除后使用HTTP缓存获取集合

[英]REST - Get collection with HTTP cache after delete

I'm building a REST API which has the followings endpoints: 我正在构建具有以下端点的REST API:

GET /api/v1/categories - Get all categories
DELETE /api/v1/categories/$id - Delete a specific category

The mobile app shall gets all categories in the first time but later, it shall gets only the last modified categories (using Last-Modified HTTP header). 移动应用程序应首次获取所有类别,但之后应仅获取最后修改的类别(使用Last-Modified HTTP标头)。

When a second app deletes a category, the server deletes it from the database. 当第二个应用程序删除类别时,服务器将从数据库中删除该类别。 After the deletion, if others apps request the last modified categories, the server returns nothing about the deleted category and those apps don't remove it from their local database. 删除后,如果其他应用请求了最后修改的类别,则服务器不会返回有关已删除类别的任何信息,并且这些应用不会将其从其本地数据库中删除。

What is the best solution to this problem? 解决此问题的最佳方法是什么?

I'm thinking to use soft delete (a flag indicating that the category was deleted) and when the apps get the last modified categories, the server returns all modified categories since the date passed in HTTP header, including the deleted categories. 我正在考虑使用软删除(指示已删除类别的标志),并且当应用程序获得上次修改的类别时,服务器将返回自HTTP标头传递的日期以来的所有修改的类别,包括已删除的类别。 However, if the apps request all categories (without Last-Modified header), the server returns all categories, except deleted (marked with the flag). 但是,如果应用程序请求所有类别(没有Last-Modified标头),则服务器将返回除已删除(标有该标志)之外的所有类别。

Is the presented solution the best one to this problem? 提出的解决方案是解决此问题的最佳方法吗?

What is the best solution to this problem? 解决此问题的最佳方法是什么?

You might look to the web for inspiration. 您可能会在网上寻找灵感。 See RFC 7234 . 参见RFC 7234

The high points being this: the web is a distributed system, and each client gets to manage their own local cache of representations of resources. 最高点是:Web是一个分布式系统,每个客户端都可以管理自己的资源表示形式的本地缓存。 Resources are delivered to the clients with metadata describing its freshness ; 资源通过描述其新鲜度的元数据交付给客户; the client is entitled to re-use their local copy of state until it is stale. 客户有权重用其本地状态副本,直到状态失效为止。 Successful unsafe operations on a resource automatically evict previously cached representations for that resource, and a mechanism ( 304 ) exists to allow the server to cheaply communicate to the client that a previously cached representation is still fresh. 对资源的成功不安全操作会自动退出该资源的先前缓存的表示,并且存在一种机制( 304 ),以允许服务器廉价地向客户端传达先前缓存的表示仍是新鲜的。

But changes on the server don't automatically propagate to all clients, nor do they cascade from one resource to another. 但是服务器上的更改不会自动传播到所有客户端,也不会从一种资源级联到另一种资源。 You need to instead think in terms of selecting the right freshness policy for each resource in isolation. 相反,您需要考虑为每个资源单独选择正确的新鲜度策略。

This is one of the reasons it is important to recognize that the design of your resource model needs to satisfy a different set of constraints than does the design of your data model. 这是重要的原因之一,认识到资源模型的设计需要满足与数据模型的设计不同的约束集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM