How do I cache (core) API requests?

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.


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.

enter image description here

Solutions Collecting From Web of "How do I cache (core) API requests?"

There 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.