Caching has been added to the RGraph forum

Written by Richard, on 12th August 2022

This may be interesting to you - I've just added caching to the RGraph forum. Two types have been added:

  1. HTTP caching headers
  2. Server-based pre-rendering

HTTP caching headers

Adding HTTP headers for caching was an easy win - simply add the relevant HTTP headers and browsers will attempt to cache the response (ie the page) that the server sends. The headers that have been added are as follows:

Cache-Control: public, private, max-age=604800
Expires: Fri, 19 Aug 2022 09:56:47 GMT

So the response is cached for a week and can be cached by your browser and also by other caches too (eg ones at your ISP or other intermediaries).

Server-based pre-rendering

This is a bit more interesting and a little bit more involved than simply adding some extra HTTP headers to the response. What is done is a script that runs every so often makes a HTTP request for the forum pages (the ones that are closed) - the resulting output is stored in a file on disk. The full HTML page that's written to disk is exactly what you see when you request the page with your browser.

Then, when you request a forum post (one that's closed), if it's in the cache it gets sent to your browser instead of the page being built from the contents of the database. So there's less load on the database (something I'm sure the ISP will thank me for). The cache is cleared and then the pages are re-cached every four hours by a cron job (a scheduled task).

If you're thinking of employing this type of caching then you need to ensure that the HTML that makes up the page doesn't change. If it does then you could end up with stale, cached copies of your pages being shown instead of up-to-date copies.