简体   繁体   English

Django Whitenoise缓存清除控件

[英]Django Whitenoise cache busting control

I've run manage.py collectstatic and Whitenoise has post-processed all of the static files. 我已经运行了manage.py collectstatic并且Whitenoise已对所有静态文件进行了后期处理。 I'm not quite sure what should i do now if i want to change/update some of the files, for example, my .css stylesheet? 我不确定如果要更改/更新某些文件(例如我的.css样式表)该怎么办? Should i run manage.py collectstatic every time files have been changed? 每次文件更改后,我都应该运行manage.py collectstatic吗? I'm asking this because my development server takes about 45 minutes to finish that task, and i'm not sure if that's normal because i have only 550 static files, 250Mb total. 我问这个问题是因为我的开发服务器大约需要45分钟才能完成该任务,而且我不确定那是否正常,因为我只有550个静态文件,总共250Mb。

Secondly, as Whitenoise doesn't support serving media files i use Amazon CloudFront for that. 其次,由于Whitenoise不支持提供媒体文件,因此我使用Amazon CloudFront。 How can i control cache busting with those media files that users have uploaded? 我该如何控制用户上传的媒体文件的缓存破坏? This is very important for me. 这对我来说很重要。

Yes, you'll need to run collectstatic every time your files change. 是的,您每次文件更改时都需要运行collectstatic

It's very unusual to have 250MB of static files. 拥有250MB的静态文件是非常不寻常的。 Also, because Django's cache busting creates a copy of each file with a unique name you'll end up with two copies of each file so that's 500MB already. 另外,由于Django的缓存清除功能会为每个文件创建一个具有唯一名称的副本,因此您最终将获得每个文件的两个副本,因此已经有500MB。 On top of this WhiteNoise will be creating gzip-compressed versions of each file so you could be heading towards 1GB of files. 在此WhiteNoise的基础上,将为每个文件创建gzip压缩版本,因此您可能会使用1GB的文件。

One quick way of speeding up this process would be to tell WhiteNoise not to compress your PDF files, which you can do by adding .pdf to the WHITENOISE_SKIP_COMPRESS_EXTENSIONS setting. 加快此过程的一种快速方法是告诉WhiteNoise不要压缩您的PDF文件,您可以通过在WHITENOISE_SKIP_COMPRESS_EXTENSIONS设置中添加.pdf完成

It sounds like your brochures would be better stored as user media though, rather than static assets. 听起来,您的小册子应该更好地存储为用户媒体,而不是静态资产。

To control caching you should make your code generate a unique name for each file when it's uploaded (adding a random string as a prefix to the filename should do the trick). 为了控制缓存,您应该使代码在上传时为每个文件生成一个唯一的名称(可以在文件名中添加一个随机字符串作为前缀)。 You can then set caching headers on these files for as long as you like. 然后,您可以根据需要在这些文件上设置缓存头。

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

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