php 部署后强制刷新缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8937447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:48:39  来源:igfitidea点击:

Force cache refresh after deployment

php.htaccessuricache-control

提问by Kyle

Is there a way to force a client's cache to reload an HTML file if you can't change the URI referencing that file (e.g., can't add a timestamp param)?

如果您无法更改引用该文件的 URI(例如,无法添加时间戳参数),是否有办法强制客户端的缓存重新加载 HTML 文件?

Here's my situation:

这是我的情况:

  • A plugin deployed to a 1000 users
  • That plugin loads example.com/page.htmlwhich calls on script.js
  • The resource URI example.com/page.htmlcannot be changed (w/o plugin updates)
  • page.htmlhas been changed. I need to clear the old page.htmlfrom users' cache so the new page.htmlcan load.
  • 部署到 1000 个用户的插件
  • 该插件加载example.com/page.html调用script.js
  • example.com/page.html无法更改资源 URI (无插件更新)
  • page.html已经变了。我需要page.html从用户的缓存中清除旧的,以便page.html可以加载新的。

Any ideas? Htaccess? The PHP API that the old & new page.htmlcall on?

有任何想法吗?访问?新旧page.html调用的 PHP API ?

Thanks!

谢谢!

回答by rdlowrey

Well, if the page is already cached by a browser it's difficult to tell it not to use its cached version because it probably won't bother to check again before it determines its cached version is stale. You'll just have to send a snail-mail letter to all of your users informing them to press ctrl+f5 :)

好吧,如果页面已经被浏览器缓存,很难告诉它不要使用它的缓存版本,因为它可能不会在确定其缓存版本过时之前再次检查。您只需向所有用户发送一封蜗牛邮件,通知他们按 ctrl+f5 :)

There is a chance that the browser might at least try a HEAD request to check the modified timestamp before it serves up its cached version, though. In this case the following will help you out.

不过,浏览器可能至少会尝试一个 HEAD 请求来检查修改后的时间戳,然后才能提供其缓存版本。在这种情况下,以下内容将帮助您。

Browsers negotiate their content from your web server using HTTP standard headers. Going forward if you want to tell a browser not to cache a file, you have to send the appropriate HTTP headers. If you want to do this in PHP you can use the headerfunction to send the appropriate HTTP headers to the browser:

浏览器使用 HTTP 标准标头从您的 Web 服务器协商其内容。以后如果你想告诉浏览器不要缓存文件,你必须发送适当的 HTTP 标头。如果您想在 PHP 中执行此操作,您可以使用该header函数将适当的 HTTP 标头发送到浏览器:

header('Cache-Control: no-cache');
header('Pragma: no-cache');

If it has to be done via HTML you can do the following in your page header:

如果必须通过 HTML 完成,您可以在页眉中执行以下操作:

<meta http-equiv="Expires" content="Tue, 01 Jan 1995 12:12:12 GMT">
<meta http-equiv="Pragma" content="no-cache">

There is no way for you to be sure if the browser will honor your request that it not cache a page, however. There are some other things like eTags and whatnot but frankly I don't think that's going to help you out if the page is already cached.

但是,您无法确定浏览器是否会满足您不缓存页面的请求。还有一些其他的东西,比如 eTags 等等,但坦率地说,如果页面已经被缓存,我认为这不会帮助你。



UPDATE

更新

From the HTTP/1.1 specification on Response Cacheability:

来自关于响应缓存能力的 HTTP/1.1 规范:

If there is neither a cache validator nor an explicit expiration time associated with a response, we do not expect it to be cached, but certain caches MAY violate this expectation (for example, when little or no network connectivity is available).

如果既没有缓存验证器也没有与响应相关的明确过期时间,我们不希望它被缓存,但某些缓存可能会违反这个期望(例如,当网络连接很少或没有可用时)。

回答by Kyle

Perhaps PHP could be used to add a timestamp to the javascript call. You could then run this for the duration...........For example:

也许 PHP 可用于向 javascript 调用添加时间戳。然后,您可以在一段时间内运行它..............例如:

check_if_cache_should_be_disabled.php

<?php
$deployment_flag = true; // Uncomment this if you want to disable normal cache.
//$deployment_flag = false // Uncomment this if you want to enable normal cache.
?>

check_if_cache_should_be_disabled.php

<?php
$deployment_flag = true; // Uncomment this if you want to disable normal cache.
//$deployment_flag = false // Uncomment this if you want to enable normal cache.
?>


page.php

<script src="/js/script.js<?php 
require('check_if_cache_should_be_disabled.php');
//  File Get Contents can be used for a remote server
//file_get_contents('http://yourserver.com/check_if_cache_should_be_disabled.php');
if ($deployment_flag == true) {
print ( '?ts='.date() );
}
?>"></script>

页面.php

<script src="/js/script.js<?php 
require('check_if_cache_should_be_disabled.php');
//  File Get Contents can be used for a remote server
//file_get_contents('http://yourserver.com/check_if_cache_should_be_disabled.php');
if ($deployment_flag == true) {
print ( '?ts='.date() );
}
?>"></script>

回答by DeepBlue

You can change the file name, for example page_v2.html which will make the borwser load the page as a new page.

您可以更改文件名,例如 page_v2.html,这将使浏览器将页面加载为新页面。