简体   繁体   English

缓存与Django破坏

[英]Cache busting with Django

I'm working on a website built with Django. 我正在使用Django构建的网站。
When I'm doing updates on the static files, the users have to hard refresh the website to get the latest version. 当我对静态文件进行更新时,用户必须硬刷新网站才能获得最新版本。
I'm using a CDN server to deliver my static files so using the built-in static storage from Django. 我正在使用CDN服务器来传递静态文件,因此使用Django内置的静态存储。
I don't know about the best practices but my idea is to generate a random string when I redeploy the website and have something like style.css?my_random_string . 我不知道最佳实践,但我的想法是在重新部署网站时生成一个随机字符串,并且有类似style.css?my_random_string
I don't know how to handle such a global variable through the project (Using Gunicorn in production). 我不知道如何通过该项目处理这样的全局变量(在生产中使用Gunicorn)。
I have a RedisDB running, I can store the random string in it and clear it on redeployment. 我有一个RedisDB运行,我可以在其中存储随机字符串并在重新部署时清除它。
I was thinking to have this variable globally available in templates with a context_processors . 我想在带有context_processors模板中全局提供这个变量。

What are your thoughts on this ? 你对此有何看法?

Django's built-in contrib.staticfiles app already does this for you; Django内置的contrib.staticfiles应用已经为您做到了这一点; see ManifestStaticFilesStorage and CachedStaticFilesStorage . 请参阅ManifestStaticFilesStorageCachedStaticFilesStorage

Here's my work around : 这是我的工作:

On deployment (from a bash script), I get the shasum of my css style. 在部署(从bash脚本),我得到了我的CSS风格的shasum。
I put this variable inside the environment. 我把这个变量放在环境中。

I have a context processor for the template engine that will read from the environment. 我有一个模板引擎的上下文处理器,它将从环境中读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM