最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rest api - How do I cache (core) API requests?

programmeradmin2浏览0评论

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
  • 1 I don't know about caching core API requests, but Memcached (memcached) seems to be a good solution for caching API requests generally. – Lucas Bustamante Commented Jul 30, 2018 at 22:19
  • I agree with using something like memcached or redis (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
Add a comment  | 

3 Answers 3

Reset to default 1

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.

This one below is actually better I believe:

https://github/airesvsg/wp-rest-api-cache

发布评论

评论列表(0)

  1. 暂无评论