Python for Entrepreneurs Transcripts
Chapter: Web design foundations
Lecture: Concept: Cache busting

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Let's quickly review this concept of cache busting. Recall we'd started out with the white site with black text
0:08 and it looked like this, more or less and then we made a change, we wanted to say "switch it", so we had light text and a dark background,
0:17 without the cache busting in place well, remember that basically had no effect,
0:21 at least under certain circumstances, not the least of which is in production. So we added this cache id query string parameter to our CSS files,
0:32 so "?cacheId=whatever", right, you can put anything after the query string question mark there, and we wrote a function called build_cache_id
0:41 and it looks at the contents of the file and it creates an md5 hash which has the really nice property of - it's pretty short and it changes
0:49 if and only if the contents of that file change. So basically, every time we change a file, we get a new URL
0:55 that also gets aggressively cached but it doesn't matter to anybody because it's always caching the new one.
1:01 So now with this in place, we no longer have that problem, when we deploy a new version or just continue to use the site
1:08 it automatically adapts to the new CSS, regardless of what the cache settings are
1:13 and they should be extremely aggressive in production on that static folder. Here is the code that we wrote in sort of simplistic,
1:20 "help you understand what is going on" version, we used the dunder name to create the full path, and then we took a relative file URL,
1:28 this is basically a web relative path, and we used out full path and a little bit of os path juggling
1:35 to come up with a local file name, verify that that worked and then we just computed the hash with our md5 hash there and returned hash digest.
1:43 Now I said in production, we really should use a dictionary and recompute this only if we'd never seen the file before for that run of the process,
1:52 so I've included in the demo code the file called static_cache.py, which is a much more production-friendly version.
2:00 It's not identical to the one we'll have in the final product, it's not identical to the one I am using on my websites, but it's really close.


Talk Python's Mastodon Michael Kennedy's Mastodon