"Stale" files are a common problem in web development. And when your app is small, the performance benefits of aggressively caching files might not be worth the complexity and potential bugs that can crop up from not handling cache behavior correctly. After all, the famous quote goes:
There are only two hard things in Computer Science: cache invalidation, naming things, and off-by-one errors.
That said, there is one more strategy I want to cover. It's my personal favorite for apps like Tubely.
In Tubely, we just don't care about old versions of thumbnails. Like ever. So let's just give each new thumbnail version a completely new URL (and path on the filesystem). That way, we can avoid all potential caching issues completely.
It's not that caching is bad generally (it's incredibly useful for many performance-related issues), but we know we don't need it for this part of this app.
One final cache update! Each time a new thumbnail is uploaded, we'll give it a new path on disk (and by extension, a new URL). This way, we can avoid all caching issues completely.
Instead of using the videoID to create the file path, use crypto/rand.Read to fill a 32-byte slice with random bytes. Use base64.RawURLEncoding to then convert it into a random base64 string. Use this string as the file name, and set the extension based on the media type (same as before). For example:
QmFzZTY0U3RyaW5nRXhhbXBsZQ.png
Run and submit the CLI tests.