I'm am currently trialling the new (as of 2017) core API built into Wordpress. My setup is reasonably simple:
+---------+ +------+ +---------+
|Wordpress|<-->|Guzzle|<-->| App |
|(API) | +------+ |(PHPSlim)|
+---------+ +---------+
Guzzle will be operating through a local loopback (/etc/hosts set up to see the api as a local resource).
The major players in the WP space for cacheing (WP Super Cache, W3, etc) don't appear to do anything around the API. My understanding is that they essentially create snapshots of a rendered page and skip over any php (including db calls) for future requests.
So...
The question is, is it possible to apply a level of cache to the API calls in WP? The site is reasonably static, so ideally I don't want to ping the DB for every request.
I have examined the headers returned by WP and no cacheing indicators are present. I have also considered using wp_cache functions or wp_transient functions, but both seem to be a misuse of their functionality.
Headers:
I'm am currently trialling the new (as of 2017) core API built into Wordpress. My setup is reasonably simple:
+---------+ +------+ +---------+
|Wordpress|<-->|Guzzle|<-->| App |
|(API) | +------+ |(PHPSlim)|
+---------+ +---------+
Guzzle will be operating through a local loopback (/etc/hosts set up to see the api as a local resource).
The major players in the WP space for cacheing (WP Super Cache, W3, etc) don't appear to do anything around the API. My understanding is that they essentially create snapshots of a rendered page and skip over any php (including db calls) for future requests.
So...
The question is, is it possible to apply a level of cache to the API calls in WP? The site is reasonably static, so ideally I don't want to ping the DB for every request.
I have examined the headers returned by WP and no cacheing indicators are present. I have also considered using wp_cache functions or wp_transient functions, but both seem to be a misuse of their functionality.
Headers:
Share Improve this question asked Mar 27, 2017 at 2:42 ChrisChris 4083 silver badges8 bronze badges 2 |3 Answers
Reset to default 1There is a cache plugin for WP Rest API with the name... WP Rest API Cache:
I've used it for small projects and helped me a lot.
It is an API.... would you like to get stale cached results when doing $i++? I guess not.
Caching should be done on the user side of the API as only it can know what is the level of staleness that can be tolerated.
It is not like you should avoid any type of caching on the wordpress side, but keep in mind that to do cache invalidation is a bitch. The safest way to go is with object caching which you should have in any case on any serious wordpress deployment.
This one below is actually better I believe:
https://github/airesvsg/wp-rest-api-cache
memcached
orredis
(redis.io). These can cache pretty much everything WordPress does. The biggest overhead in any request is the database, so you really want to be caching that and not the REST API itself (unless you have some oddly-specific use-case). If the reason you want caching is because there's too much strain on your server, then anything that caches the database should sufficiently help alleviate that strain. – phatskat Commented Apr 2, 2019 at 1:04