0

I would like to use REST API to update custom fields on various pages - an example would be tracking outside temperature, humidity, etc from a sensor. However, if the website has cache, the pages won't update with the new information in the custom fields.

How can I "punch holes" in the cache so only these fields are updated without regenerating the entire cache for the page?

I thought of object cache but that is for DB queries and probably won't be suitable for this purpose.

Any ideas are welcome. Many thanks

EDIT: So far I can think of two options:

  1. Scrap and recreate the entire cache
  2. Load the custom fields with AJAX
  3. Server Side Events

I am not sure which is faster. Perhaps it depends on the number of visitors and the frequency of data updates which might cause massive amounts of AJAX calls while the cache is created only once. What is the standard approach in situations where you want to present up-to-date information on the website?

3
  • how is this cache implemented? WP doesn't come with fullpage caching out of the box, and object caching requires a drop in with a supported service
    – Tom J Nowell
    Jun 24, 2021 at 13:22
  • @TomJNowell I haven't built it yet because I am looking for the optimal approach
    – Viktor
    Jun 24, 2021 at 13:37
  • Then I don't believe you can find an answer to this, the optimal answer will be highly specific to the technology you use to implement your cache. There is no general WordPress caching mechanism that works for everything. E.g. the solution for Varnish would be wildly different to the solution for W3TC, which would again be completely different to a site that relied on batcache, and then again for a cloudflare cache
    – Tom J Nowell
    Jun 24, 2021 at 13:48

2 Answers 2

0

The closest to what you want is the wp cache flush CLI command, or wp_cache_flush() which will flush any object caches, but these only flush persistent object caches implemented via WP_Cache.

In particular:

  • this only applies to object caches. If you do not have a persistent object cache then it will have no effect
  • CDN caches and software such as Varnish caches can't be flushed this way
  • Page caching plugins that use the disk can't be flushed this way
  • caching plugins won't support it, each plugin requires a unique solution
  • transients and other caches in the database will be unaffected
  • caches used by plugins that don't use the object cache will be uneffected
  • wp cache flush flushes the entire object cache, not a specific page or post/meta

So for anything that isn't the persistent object cache, you're out of luck, and will need to investigate the specific solutions you have chosen for your site. No generic WordPress solution exists

0

Doing AJAX the wordpess way will basically leave your site without caching, as there is a huge penalty to load wordpress in order to handle a request and it can take time which is similar to loading an uncached page. You will end with a process which first load the cached page and immidiatly load the AJAX ending with something that actually takes longer than an uchached page.

The only way to use AJAX if it is triggered on demand, some kind of user action. If you can design your UX in such a way, it is a possible solution.

The other option which is relevant to sensor based information is to avoid contacting wordpress for the information. You can probably arrange all the "real time" data as a flat or json file and place it in a place in which it is accessable from the web and load it (wrap it us JS, and you can just include it as a script.

This approch avoid conecting to the DB and initializing wordpress.

I assume that to read the sensors you use some kind of shell script that you can change and control what it does with the output.

2
  • Thanks this sounds very sensible! In fact, it will be a python script posting to the REST API. What solution would you recommend for storing the information in an AJAX-friendly way instead? Ideally, it wouldn't be just a publicly available file that everyone can download or scrape.
    – Viktor
    Jun 24, 2021 at 14:58
  • well, everything on the web is practically public and can be scarped so I would not worry too much about it. It is true that AJAX of any kind makes it easier to scrap, but it is not too hard without it. but if you use a script which is activated by cron, lets say every 5 minutes, you can explore the option of "micro caching", cache the pages only for few minutes. You can also differentiate between the handling of historical data which requires storage in DB and real time data. Jun 24, 2021 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.