Skip to content

Instantly share code, notes, and snippets.

@codigoconjuan
Created February 17, 2018 20:14
Show Gist options
  • Save codigoconjuan/25e07184d1b432d7df693eb2e65c9985 to your computer and use it in GitHub Desktop.
Save codigoconjuan/25e07184d1b432d7df693eb2e65c9985 to your computer and use it in GitHub Desktop.
Cachear páginas PHP a HTML
<?php
// Definir un nombre para cachear
$archivo = basename($_SERVER['PHP_SELF']);
$pagina = str_replace(".php", "", $archivo);
// Definir archivo para cachear (puede ser .php también)
$archivoCache = 'cache/'.$pagina.'.html';
// Cuanto tiempo deberá estar este archivo almacenado
$tiempo = 36000;
// Checar que el archivo exista, el tiempo sea el adecuado y muestralo
if (file_exists($archivoCache) && time() - $tiempo < filemtime($archivoCache)) {
include($archivoCache);
exit;
}
// Si el archivo no existe, o el tiempo de cacheo ya se venció genera uno nuevo
ob_start();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment