如何优化 REST API 调用 [英] How to Optimize REST API calls

查看:34
本文介绍了如何优化 REST API 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建 Mash up 移动应用程序.我需要调用我的 API Provider 并与 Facebook、twitter 等集成.在这个过程中,我必须对同一个域一个接一个地进行多个 REST API 调用(当然具有不同的路径和查询参数).此外,API 调用必须是顺序的,因为需要一个调用下一​​个.我可以通过哪些方式优化这些 http 调用以避免往返.欢迎对 java 和 js 提出建议

I am in process of building Mash up mobile application. I need to call my API Provider and integrate with Facebook , twitter etc. During this process, I have to make multiple REST API calls one after other to same domain(with different path and query parameters ofcourse). Also the API calls has to be sequential as in result of one is required to call the next. What are the ways I can optimize these http calls to avoid the round trip. Suggestions for java and js is welcome

推荐答案

所述的要求很广泛.由于您使用的是公共第三方 API,这在一定程度上限制了可能优化的范围.您绝对无法加速 API,因为它们不属于您.

The requirements stated are broad. Since you are using the public third party API, that somewhat limits the scope of possible optimizations. There is absolutely nothing that you can do to speed up the APIs as they do not belong to you.

总的来说,我建议遵循以下指南,这将有助于您提出更好的应用程序.

In general I suggest following guidelines which will help you come up with better application.

  1. Thumb 规则是尽可能少地调用 API.
  2. 减少网络调用(由 API 调用引起)意味着设备无线电(用于与服务器无线通信)的使用频率将降低,因此需要的功率也更少.
  3. 现在,减少通话次数可能并不总是可行的.因此,仔细研究公共 API 文档,确保您了解所需 API 的所有详细信息.
  4. 例如,可能有一个 API,可在传递实体标识符时提供实体的详细信息.如果您需要 5 个这样的实体的详细信息,那么应该有一个 API,它接受一堆 ID 并一次返回所有详细信息.这样您可以节省多次通话.
  5. 缓存是减少 API 调用次数的非常有效的方法.因此,查找从服务器收到的任何可以缓存一段时间的数据.
  6. 请注意,缓存需要更多内存,因此请确保您了解其含义并相应地决定需要缓存的数据量.
  7. 除了内存,数据也可以缓存在磁盘上,这将有助于减少内存使用.不确定您是否使用 Android,请参考此相关帖子.
  1. Thumb rule is make as few API calls as possible.
  2. Making fewer network calls (resulting from API calls) will imply that the device radio (which is used to communicate with the server over the air) will be less frequently used and hence less power required.
  3. Now, making fewer number of calls may not be an option always. So study the public API documentation carefully and make sure you know the all the details of the APIs you need.
  4. For example, there could be an API which gives details of an entity upon passing its identifier. If you need the details of 5 such entities then there should be an API which takes a bunch of IDs and returns all the details at once. This way you save multiple calls.
  5. Caching is very effective way of reducing the number of API call. So look for any data received from server which can be cached for certain duration.
  6. Note that caching would need more memory and hence make sure you are aware of its implication and accordingly decide the amount of data that needs to cached.
  7. Apart from memory, the data can be cached on the disc as well, which will help reduce the memory usage. Not sure if you are using Android, please refer to this related post.

在旁注中,您可以参考以下链接,这些链接提供了开发移动应用程序的一般指南.

On the side note, you can refer to following links which offer general guidelines for developing a mobile App.

  1. 让您的应用程序快速运行.
  2. 针对移动网络优化移动应用.

请注意,这些是一般准则.有些可能不适用于您的特定用例.

Please note that these are general guidelines. Some may not apply to your particular use case.

这篇关于如何优化 REST API 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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