<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[cache php]]></title><description><![CDATA[<p dir="auto">Salve ragazzi, mi sono imbattuto da poco nel provare una classe per la cache del mio sito web, classe che ho trovato durante una ricerca sul web ma che non sto riuscendo a capire il suo funzionamento.<br />
In pratica se ho la pagina pippo/pelo-1196.  html e voglio salvare la cache della pagina nella directory: ita/cache/-1196.  html come dovrei strutturare lo script?</p>
<pre><code class="language-php">cache.class.php
  
&lt; ?php
/*
* Clase PHP - Cache

*/
class cache
{
var $cache_dir; // path ó ruta donde se almacena la cache
var $cache_time; // tiempo en que expira la cache (en segundos)
var $caching = true; //true, per la cache
var $cleaning = false; //true, para limpiar y actualizar
var $file = 'cache'; // percorso cartella file cache
 
function iniziacache($path='',$time,$action=NULL){
global $_SERVER;
$this-&gt;cache_dir = $path;
$this-&gt;cache_time = $time;
$this-&gt;cleaning = $action;
$this-&gt;file = $this-&gt;cache_dir."cache_"
.md5(urlencode($_SERVER['REQUEST_URI'])); //md5
//condicional: Existencia del archivo, fecha expiración, acción
if (file_exists($this-&gt;file) &amp;&amp; (fileatime($this-&gt;file)
+$this-&gt;cache_time)&gt;time() &amp;&amp; $this-&gt;cleaning == false){
readfile($this-&gt;file);
exit();
} else {
$this-&gt;caching = true;
//grabamos buffer
ob_start();
}
}
 
function chiudicache(){
if ($this-&gt;caching){
//Recuperamos información del buffer
$data = $this-&gt;ob_get_clean2();
// mostramos información
echo $data;
//borramos cache si existe
if(file_exists($this-&gt;file)){
unlink($this-&gt;file);
}
 
//escribimos información en cache
$fp = fopen( $this-&gt;file , 'w' );
fwrite ( $fp , $data );
fclose ( $fp );
}
}
 
function ob_get_clean2() {
    $ob_contents = ob_get_contents();
    ob_end_clean();
    return $ob_contents;
}
} // Fin clase Cache
?&gt;
  Quello che a noi interessa è il parametro
  
var $file = 'cache'; // percorso cartella file cache
  dove andiamo ad impostare il percorso della cartella dove andremo a  mettere i file della cache.
 Funzionamento
Il funzionamento è semplice ed intuitivo, ecco un bel esempio:
  
&lt; ?
include('cache.class.php');
//instanziamo la cache
$cache1 = new cache();
 
//no cache
echo "Senza cache: ". date("D M j G:i:s T Y") . "&lt;br&gt;";
 
//con cache
$cache1-&gt;iniziacache('cache/',60,false);
echo "Con cache: ". date("D M j G:i:s T Y")."";
 
//chiudicache
$cache1-&gt;chiudicache();
?&gt;
  Upgrade! Aggiornamento e cancellazione della  cache
 Ogni 10 min svuotiamo la cache
  
$tempo=(time()  % (10*60));       //10 min
if($tempo==0){
   $rep=opendir('../cache/');
   while ($file = readdir($rep)) {
        if($file != '..' &amp;&amp; $file !='.' &amp;&amp; $file !=''){
                        unlink("../cache/$file");
   }}
   closedir($rep);
}
?&gt;
</code></pre>
]]></description><link>https://dev.connect.gt/topic/125798/cache-php</link><generator>RSS for Node</generator><lastBuildDate>Thu, 26 Nov 2026 06:32:10 GMT</lastBuildDate><atom:link href="https://dev.connect.gt/topic/125798.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 19 Apr 2010 14:21:54 GMT</pubDate><ttl>60</ttl></channel></rss>