Vincent's Site

Notes on Caching content

Date: 2024-12-17
Category: Misc

In a previous post I explained how to add caching to Django (and in a future post I'll explain how to add it to a webserver like Nginx or Apache). I experimented with caching and did some reading on the matter and I learned a few things.

there are two things hard in computer science: cache invalidation and naming things
-- Phil Karlton

Layers a web requests can be cached

So, how long do we need to set our TTL

This all depends on your usecase. I advice to keep it short. It's better to re-render the page every 10 minutes than having trouble invalidating caches somewhere you can't control and having users seeing super out of date content.

For this blog, I decided to set a TTL of 10 minutes for pages/posts on client caches / downstream caches. I do cache pages and posts for an hour on the server, but I added some code that invalidates the entries whenever I add a new post or page. Even if for some reason this invalidation fails, an hour is not that long for a blog. 

For the rewrite of Skyz I'm still deciding, but I will probably do something allong the lines of this:

CSS/JS assets can be cached for a longer period, for example a week or even a month, but you probably want to use versioned URL's (this is called cachebusting). 

 

Previous post Next post