[英]Forcing cache update in browser
I'm using Minify ( https://code.google.com/p/minify/ ) to compress and combine ~30 css files and ~10 javascript files. 我正在使用Minify( https://code.google.com/p/minify/ )压缩并合并约30个CSS文件和约10个JavaScript文件。 I've created a group configuration for those files, which is basically an array with the filenames.
我为这些文件创建了一个组配置,基本上是一个带有文件名的数组。
Works like a charm, except when one of the scripts is modified: browser cache is not timely update. 就像一个超级按钮一样工作,除非修改了其中一个脚本:浏览器缓存未及时更新。 I used to get a last modified timestamp for each file (using filemtime) to overrule browser caching:
我曾经获得每个文件的最后修改时间戳(使用filemtime)以否决浏览器缓存:
$time = '?' . filemtime( $filename );
$output = '<link type="text/css" rel="stylesheet" href="'.$file_path.'?'.$time.'" />';
I could loop through all 40 files to get the latest timestamp, but that is a very ugly solution. 我可以遍历所有40个文件以获得最新的时间戳,但这是一个非常丑陋的解决方案。 Another way would be eg to have a crobjob check it and write the timestamp to a file, which I can then include.
另一种方法是例如让crobjob检查它并将时间戳记写入文件,然后可以将其包括在内。
Any other ways before I'm inventing the wheel again? 在我再次发明轮子之前还有其他方法吗?
您可以不只是添加:
src="path/to/file.css?v=<?php date(dmy); ?>"
Just get the modification time of the $file_path
...? 只是获得
$file_path
的修改时间...? Personally, I have a much more reasonable number of files (two or three) and each one is individually cached with its mtime
. 就我个人而言,我的文件数量要合理得多(两个或三个),每个文件都使用
mtime
单独缓存。 Works beautifully. 做工精美。
I can suggest you a way that you need to define a rewrite rule and implement a handler for static file load. 我可以建议您一种需要定义重写规则并实现静态文件加载处理程序的方式。 Let say it is
assethandler.php
, and your rewrite rule; 假设它是
assethandler.php
,并且是您的重写规则;
RewriteRule ^nocache/(.*?)$ assethandler.php?f=$1 [QSA,L]
And in php you can use Cache-Control and Expires for getting always latest file; 在php中,您可以使用Cache-Control和Expires来获取最新文件;
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 01 Jul 1990 05:00:00 GMT"); // past date
readfile('path_to_static_files/' . $_GET['f']);
and your static file requests will be like; 您的静态文件请求将像;
<script src="nocache/your.js"/>
Simply, when you make a request to nocache/filename
this will be handled as assethandler.php?f=filename
and in this handler, cache always disabled and file content served as latest content 简而言之,当您向
nocache/filename
发出请求时,将其作为assethandler.php?f=filename
进行处理,并且在此处理程序中,缓存始终处于禁用状态,文件内容用作最新内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.