È possibile effettivamente salvare l'output della pagina prima di terminare lo script, quindi caricare la cache all'inizio dello script.
codice di esempio:
<?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);
?>
Se si dispone di un sacco di pagine che necessitano di questo caching si può fare questo:
in cachestart.php
:
<?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();
?>
in cacheend.php
:
<?php
$c = ob_get_contents();
file_put_contents($cachefile);
?>
Poi basta semplicemente aggiungere
include('cachestart.php');
all'inizio di script. e aggiungi
include('cacheend.php');
alla fine dei tuoi script. Ricordarsi di avere una cartella denominata cache e consentire a PHP di accedervi.
Inoltre, ricorda che se stai facendo una cache a pagina intera, la tua pagina non dovrebbe avere una visualizzazione specifica SESSION (ad esempio visualizzare la barra dei membri o cosa) perché saranno anche memorizzati nella cache. Guarda un framework per il caching specifico (variabile o parte della pagina).
Si sta utilizzando un framework come Zend Framework? Dacci maggiori dettagli! – Thomaschaaf
Non sto usando nessun framework. – user187580
potresti prendere in considerazione di spostarlo in un framework. http://code.google.com/p/samstyle-php-framework - framework completo che include il caching dei file e supporta memcache – mauris