Skip to content

Instantly share code, notes, and snippets.

@goedecke
Created May 22, 2016 02:45
Show Gist options
  • Save goedecke/8444c773adecb06783c1c5fe7bf595da to your computer and use it in GitHub Desktop.
Save goedecke/8444c773adecb06783c1c5fe7bf595da to your computer and use it in GitHub Desktop.
Simple Sistema de Almacenamiento de Caché
<?php
// Define la ruta y nombre del archivo que se almacenara el cache
$cachefile = 'cached-files/'.date('M-d-Y').'.php';
// define el tiempo que queremos esperar el archivo en segundos.Yo puse el mio en 5 horas.
$cachetime = 18000;
// Revisa si el cache sigue fresco (activo). En el caso de que si lo incluye y sale
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
include($cachefile);
exit;
}
// Si no hay archivo o es demasiado viejo. Renderiza la pagina y muestra el HTML
ob_start();
?>
<html>
todo tu html aca
</html>
<?php
// Listo!. Guardamos el cache en un archivo
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
// finalmente se lo enviamos al navegador
ob_end_flush();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment