如何实现稳健的分页用一个RESTful API时,该结果可以改变? [英] How to implement robust pagination with a RESTful API when the resultset can change?

查看:1255
本文介绍了如何实现稳健的分页用一个RESTful API时,该结果可以改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我执行那些通过ResultSet公开订单作为一种资源,支持分页一个RESTful API:

<?pre> GET /命令启动= 1&安培;结束= 30

,其中订单分页是由 ordered_at 时间戳排序,降序。这基本上是从SO问题分页在REST Web应用程序方式#1

如果用户请求订单的第二页( GET /订单开始= 31安培;末= 60 ),服务器只需重新​​查询订单表,分类通过 ordered_at DESC 再次返回岗位31中的记录60。

我的问题是:如果结果的变化(例如新订单添加),而用户正在查看的记录会发生什么?在添加新秩序的情况下,用户将看到旧秩序中排名第30第一的位置结果的第二页上(因为相同的顺序是现在排名第31)。更糟的是,在缺失的情况下,用户会看到旧秩序中排名第32第一位置,第二页(#31)上,并不会看到旧秩序#31(现排名第30)都没有。

我看不到解决这个不以某种方式使REST风格的服务器状态(URG)或建造一些分页智能技术应用到每一个客户端...什么是一些成熟的技术处理呢?

有关完整性:我的后端是Scala实现/喷涂/ Squeryl / Postgres的;我建立两个前端的客户端,一个在Backbone.js的,另一个在Python Django的。


解决方案

我要做到这一点,是使指数从旧到新的。所以他们永远不会改变。然后没有任何启动参数查询时,返回的最新页面。另外,响应应该包含说明都包含什么元素的索引,这样你就可以计算出你需要请求下一个页面年长的指数。虽然这是不是你想要什么,这似乎是最简单和干净的解决方案给我。

初始请求 GET /订单数= 30 的回报:

  {
  开始= 1039;
  伯爵= 30;
  ...//数据
}

从这个消费计算,他要请求:

后续请求: GET /订单开始= 1009&放大器;计数= 30 然后返回

  {
  开始= 1009;
  伯爵= 30;
  ...//数据
}

而不是原始指标,你也可以返回一个链接到下一个页面:

  {
  下一个=?/订单开始= 1009&放大器;计数= 30;
}


本办法打破,如果项目被插入或在中间删除。在这种情况下,你应该使用一些汽车持续递增值,而不是一个索引。

I'm implementing a RESTful API which exposes Orders as a resource and supports pagination through the resultset:

GET /orders?start=1&end=30

where the orders to paginate are sorted by ordered_at timestamp, descending. This is basically approach #1 from the SO question Pagination in a REST web application.

If the user requests the second page of orders (GET /orders?start=31&end=60), the server simply re-queries the orders table, sorts by ordered_at DESC again and returns the records in positions 31 to 60.

The problem I have is: what happens if the resultset changes (e.g. a new order is added) while the user is viewing the records? In the case of a new order being added, the user would see the old order #30 in first position on the second page of results (because the same order is now #31). Worse, in the case of a deletion, the user sees the old order #32 in first position on the second page (#31) and wouldn't see the old order #31 (now #30) at all.

I can't see a solution to this without somehow making the RESTful server stateful (urg) or building some pagination intelligence into each client... What are some established techniques for dealing with this?

For completeness: my back-end is implemented in Scala/Spray/Squeryl/Postgres; I'm building two front-end clients, one in backbone.js and the other in Python Django.

解决方案

The way I'd do it, is to make the indices from old to new. So they never change. And then when querying without any start parameter, return the newest page. Also the response should contain an index indicating what elements are contained, so you can calculate the indices you need to request for the next older page. While this is not exactly what you want, it seems like the easiest and cleanest solution to me.

Initial request: GET /orders?count=30 returns:

{
  "start"=1039;
  "count"=30;
  ...//data
}

From this the consumer calculates that he wants to request:

Next requests: GET /orders?start=1009&count=30 which then returns:

{
  "start"=1009;
  "count"=30;
  ...//data
}

Instead of raw indices you could also return a link to the next page:

{
  "next"="/orders?start=1009&count=30";
}


This approach breaks if items get inserted or deleted in the middle. In that case you should use some auto incrementing persistent value instead of an index.

这篇关于如何实现稳健的分页用一个RESTful API时,该结果可以改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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