简体   繁体   English

在访问者开发后清除缓存

[英]Clearing cache after development for visitor

I'm developing sites and some visitor's browsers appear with old cache. 我正在开发网站,一些访问者的浏览器显示旧缓存。

Is there a way we can clear visitor's browser cache using codes from the server side or even javascript so they don't have to clear themselves? 有没有办法我们可以使用服务器端的代码甚至是javascript来清除访问者的浏览器缓存,这样他们就不必清除自己了?

I cannot find the direct answer to this. 我找不到直接的答案。

There must be a way big companies do like Facebook, Ebay etc 大公司必须有像Facebook,Ebay等那样的方式

We have been using htaccess to determine the caching rules of the clients. 我们一直在使用htaccess来确定客户端的缓存规则。 We explicitly give the cache a 24h lifetime and we put no-cache rules the day before we do the update. 我们明确地给缓存提供了24小时的生命周期,并且在我们进行更新之前的一天我们放置了无缓存规则。 It has helped but it is tedious and not so reliable. 它有所帮助,但它很乏味,而且不那么可靠。

Just posting it to give you ideas if no one answers, but I would really love to get the answer too. 如果没有人回答,只是张贴它给你的想法,但我真的很想得到答案。 :) :)

First Method: 第一种方法:

You can actually save the output of the page before you end the script, then load the cache at the start of the script. 实际上,您可以在结束脚本之前保存页面的输出,然后在脚本的开头加载缓存。

example code: 示例代码:

<?php

$cachefile = 'cache/'.basename($_SERVER['PHP_SELF']).'.cache'; // e.g. cache/index.php.cache
$cachetime = 3600; // time to cache in seconds

if(file_exists($cachefile) && time()-$cachetime <= filemtime($cachefile)){
  $c = @file_get_contents($cf);
  echo $c;
  exit;
}else{
  unlink($cachefile);
}

ob_start();

// all the coding goes here

$c = ob_get_contents();
file_put_contents($cachefile);

?>

You can actually save the output of the page before you end the script, then load the cache at the start of the script. 实际上,您可以在结束脚本之前保存页面的输出,然后在脚本的开头加载缓存。

example code: 示例代码:

If you have a lot of pages needing this caching you can do this: 如果您有很多需要此缓存的页面,您可以这样做:

in cachestart.php: 在cachestart.php中:

in cacheend.php: 在cacheend.php中:

<?php

$c = ob_get_contents();
file_put_contents($cachefile);

?>

Then just simply add 然后只需添加即可

include('cachestart.php'); 包括( 'cachestart.php'); at the start of your scripts. 在脚本的开头。 and add 并添加

include('cacheend.php'); 包括( 'cacheend.php'); at the end of your scripts. 在脚本的最后。 Remember to have a folder named cache and allow PHP to access it. 记住要有一个名为cache的文件夹,并允许PHP访问它。

Also do remember that if you're doing a full page cache, your page should not have SESSION specific display (eg display members' bar or what) because they will be cached as well. 另外请记住,如果您正在进行整页缓存,那么您的页面不应该具有特定于SESSION的显示(例如,显示成员的栏或什么),因为它们也将被缓存。 Look at a framework for specific-caching (variable or part of the page). 查看特定缓存的框架(变量或页面的一部分)。

Second Method: 第二种方法:

Use Squid or update the HTTP headers correctly to do browser caching. 使用Squid或正确更新HTTP标头以执行浏览器缓存。

PEAR has a caching package (actually two): PEAR有一个缓存包(实际上是两个):

http://pear.php.net/package/Cache http://pear.php.net/package/Cache

Fourth Method: 第四种方法:

Use http://memcached.org/ . 使用http://memcached.org/ There's an explanation of how to do it on that site. 有关于如何在该网站上进行此操作的说明。

I usually use a combination of techniques: 我通常使用一系列技术:

  • HTML resulting from PHP code is not cached using the standard configuration, because it sends out the appropriate headers automatically. PHP代码生成的HTML不使用标准配置进行缓存,因为它会自动发送相应的标头。
  • Images and other binary assets get renamed if they change. 如果图像和其他二进制资产发生更改,则会重命名。
  • For JavaScript and CSS I add a automatically created unique code (ea MD5 hash of the contents or the file size) to the filename (eg /public/styles.f782bed8.css ) and remove it again with mod_rewrite. 对于JavaScript和CSS,我将自动创建的唯一代码(内容的MD5哈希值或文件大小)添加到文件名(例如/public/styles.f782bed8.css )并使用mod_rewrite再次删除它。 This way every change in the file results in a new file name. 这样,文件中的每个更改都会生成一个新文件名。 This can be done at runtime in PHP while outputting the HTML header, to have it fully automated. 这可以在运行时在PHP中完成,同时输出HTML标头,使其完全自动化。 In this case however an MD5 might have a performance impact. 但是,在这种情况下,MD5可能会对性能产生影响。

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

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