简体   繁体   English

PHP中的RESTful API-优化后续请求?

[英]RESTful API in PHP - optimising successive requests?

I'm working (for the first time) on developing a PHP application driven by a PHP RESTful API (probably using peej/Tonic). 我正在(第一次)开发由PHP RESTful API(可能使用peej / Tonic)驱动的PHP应用程序。 Coming from an application with direct access which might make 20 different database calls during the course of a page load, I am trying to reconcile the fact that 20 API calls = 20x handshakes (which can be improved by Guzzle persistent connections) but also 20x connections to the database. 我来自一个具有直接访问权限的应用程序,该应用程序可能在页面加载过程中进行20个不同的数据库调用,我试图调和20个API调用= 20x握手(可以通过Guzzle持久连接进行改进)但也可以进行20x连接的事实到数据库。

I believe that with better programming and planning, I can get my required API calls down to 4-5 per page. 我相信,通过更好的编程和计划,我可以将所需的API调用降低到每页4-5个。 At this point: 这一点:

a) Is it not worth considering the latency of 5x database connections + 5x handshakes per page load on account of all the other available optimisations? a)考虑所有其他可用的优化措施,是否值得考虑5x数据库连接的延迟+每页加载5x握手的延迟?

b) Is there an existing method by which this can be mitigated that I've thus far failed to find? b)是否有一种现有的方法可以缓解这种情况,而到目前为止我还没有找到?

c) I believe it violates the principles of RESTful programming but if I had a single API method which itself gathered information from other API endpoints (for instance, GET suppliers WHERE x=y then GET products for supplier), is there a documented method for internal API interaction (particularly within peej/Tonic or other frameworks). c)我认为它违反了RESTful编程的原则,但是如果我有一个API方法本身会从其他API端点收集信息(例如,GET供应商WHERE x = y,然后是供应商GET产品),是否有用于内部API交互(尤其是在peej / Tonic或其他框架内)。

Thank you all for your wisdom in advance. 预先感谢大家的智慧。

Remember that the client should be "making 'a request' of the server," which is obliged to fulfill "that request." 请记住, 客户端应“向服务器发出“请求”,这必须履行“该请求”。 The server might "execute 20 different database queries" to prepare its response, and the client need not know nor care. 服务器可能会“执行20个不同的数据库查询”以准备其响应,并且客户端无需知道也不必关心。

The client's point-of-view becomes, "I care what you tell me, not how you did it." 客户端的点的角度的,“我不在乎你告诉我什么 ,而不是你是怎么做到的。”

If you did wish to send query-responses directly to the client, for the client to "do the dirty-work" with these data, then you could still design your server request so that the server did many queries, all at once, and sent all of the result-sets back ... in just one exchange. 如果您确实希望直接将查询响应发送给客户端,以使客户端使用这些数据“完成工作”,那么您仍然可以设计服务器请求,以便服务器一次完成许多查询,并且仅通过一次交换就将所有结果集发回了。

Your first priority should be to effectively minimize the number of exchanges that take place. 您的首要任务应该是有效地减少交换的次数。 The amount of data returned (within reason) is secondary. 返回的数据量(在合理范围内)是次要的。

Also consider that, "when the server does it, the work is naturally synchronized." 还要考虑“ 服务器完成后,工作自然就会同步”。 When the client issues multiple asynchronous requests, those requests are, well, "asynchronous." 当客户端发出多个异步请求,这些请求, 那么,“异步”。 Consider which strategy will be easier for you to debug. 考虑哪种策略更容易调试。

If the server is given "a request to do," it can validate the request (thus checking for client bugs), and perform any number of database operations, perhaps in a TRANSACTION . 如果为服务器提供了“执行请求”,则它可以验证该请求(从而检查客户端错误),并可以在TRANSACTION执行任何数量的数据库操作。 This strategy, which puts the server into a very active role, is often much less complex than an interaction that is driven by the client with the server taking a passive role. 这种将服务器置于非常主动的角色的策略通常比由客户端驱动的交互作用与服务器充当被动角色的交互简单得多。

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

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