Reputation: 2183
I am analyzing/troubleshooting a custom WordPress plugin that was developed for one client that implements a REST API to be used with a 3rd party provider. One of the requirements is to have all API calls executed in less than 1 second.
Currently the plugin offers registers 2 routes: "get user" and "create user". For the sake to make this question simple, I will focus on the first route as it is the simpler one.
The average execution time for "get user" is around ~0.5 seconds BUT the first call takes always more time up to 5 seconds. All further calls are always in the ~0.5 seconds range. If the API is not used for around 1 minute, then the next first call is again taking up to 5 seconds to be completed.
This makes me believe that there is some sort of cache available for a limited period of time (~1 second) or, that the hosting (SiteGround) / database has a sleep policy but I was not able to confirm it. To find it out, I did create a custom plain PHP script that mimics the same functionality as "get user" and the average response is ~0.15 seconds BUT also the first call takes more than the average: ~0.6 seconds.
The custom script could be one possible solution for our case but we would like to first to exhaust all the possibilities to make the WordPress plugin + REST API to work in less than 1 second if possible.
Upvotes: 0
Views: 2285
Reputation: 21
There is a caching system for the WordPress API, and I'll refer you to this article: https://css-tricks.com/the-deal-with-wordpress-transients/
But there are a few things you could potentially do to improve performance, first of which is to examine your host. Depending on your provider, you could be running into some issues with response time that aren't cached.
Another, much more complex solution is to write your own custom API responses from WP. There are plugins that might help with streamlining the response from the out-of-the-box endpoints, here's one: https://wordpress.org/plugins/custom-api-for-wp/
If you go this route, make sure you follow good security practices for WP and PHP.
Upvotes: 2