简体   繁体   中英

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?

I cannot find the direct answer to this.

There must be a way big companies do like Facebook, Ebay etc

We have been using htaccess to determine the caching rules of the clients. We explicitly give the cache a 24h lifetime and we put no-cache rules the day before we do the update. 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:

in cacheend.php:

<?php

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

?>

Then just simply add

include('cachestart.php'); at the start of your scripts. and add

include('cacheend.php'); at the end of your scripts. Remember to have a folder named cache and allow PHP to access it.

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. 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.

PEAR has a caching package (actually two):

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

Fourth Method:

Use 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.
  • 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. 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. In this case however an MD5 might have a performance impact.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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