简体   繁体   English

网站更新缓存

[英]website update cache

If you have a live website (using PHP,MySQL,JS,CSS) and say you change some functionality. 如果您有一个在线网站(使用PHP,MySQL,JS,CSS),并说要更改某些功能。 What is the best way to make sure, that when a user visits the site, the changed pages are updated and not loaded from cache? 确保用户访问网站时,更改的页面是否更新而不是从缓存加载的最佳方法是什么?

For that reson there is file loaders made for. 对于这种共鸣,有文件加载器。 When you load css, js or any other file, you must set cache control header. 加载css,js或任何其他文件时,必须设置缓存控制标头。 You can do it by editing .htaccess or with php. 你可以通过编辑.htaccess或php来实现。

.htaccess demo .htaccess演示

<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A300
ExpiresByType image/x-icon A2592000
ExpiresByType application/x-javascript A3600
ExpiresByType text/css A3600
ExpiresByType image/gif A604800
ExpiresByType image/png A604800
ExpiresByType image/jpeg A604800
ExpiresByType text/plain A300
ExpiresByType application/x-shockwave-flash A604800
ExpiresByType video/x-flv A604800
ExpiresByType application/pdf A604800
ExpiresByType text/html A300
</IfModule>

PHP demo: PHP演示:

 $offset = 3600 * 24;
 $expire = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
 header($expire);

Via HTML, in the Head section put... 通过HTML,在Head部分放...

<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">

You can't really control the policy each browser, if it complies to the standards, but what commonly happens is the browser does download the header from each HTML before deciding whether or not it should be loaded from cache or re-downloaded. 如果每个浏览器符合标准,则无法真正控制策略,但通常会发生的事情是浏览器会在确定是否应从缓存加载或重新下载之前从每个HTML下载标头。

Check RFC HTTP 1.1 Header Field for more details (under cache control in particular) 检查RFC HTTP 1.1标头字段以获取更多详细信息(特别是在缓存控制下)

Unless you have some form of caching the user will be requesting data and checking if it has been updated. 除非您有某种形式的缓存,否则用户将请求数据并检查它是否已更新。 Usually your PHP is not cached unless you have active caching. 通常,除非具有活动缓存,否则不缓存PHP。

Your JS and CSS would have to be set via HEADERS under say Apache (or whatever web server you utilize). 您的JS和CSS必须通过HEADERS设置,例如Apache(或您使用的任何Web服务器)。

You can use the window.applicationCache.swapCache(); 您可以使用window.applicationCache.swapCache(); and window.location.reload(); 和window.location.reload(); to force reload the page once javascript confirms that there's an updated version of the site. 一旦javascript确认网站的更新版本,强制重新加载页面。

A better option may be to prompt the user in some way, letting them know that the site updated. 更好的选择可能是以某种方式提示用户,让他们知道网站已更新。 That way, the page doesn't just suddenly reload. 这样,页面不会突然重新加载。

function updateCache(){
    window.applicationCache.swapCache();
    window.location.reload();
}

window.applicationCache.addEventListener("updateready", updateCache, false);

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

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