Skip to content

Instantly share code, notes, and snippets.

@raul782
Created November 22, 2012 20:35
Show Gist options
  • Save raul782/4132838 to your computer and use it in GitHub Desktop.
Save raul782/4132838 to your computer and use it in GitHub Desktop.
simple cache php
<?php
if(!$frontCache->isCached()) {
$frontCache->cachePage();
}
<?php
class FrontCache
{
private $page;
private $dir;
private $expirationTime;
public function __construct($page, $dir='cache', $expirationTime = 86400)
{
$this->page = $page;
$this->dir = $dir;
$expirationTime = 86400;
}
public function startCaching()
{
ob_start();
}
public function cachePage()
{
$cachedPage = $this->getCachedPage()
<<<EOF
</div>
</div>
<script type = "text/javascript">
setSelectSku(1);
</script>
EOF;
$fp = @fopen($cachedPage,'w');
fwrite($fp, ob_get_contents());
fclose();
ob_end_flush();
}
public function outputCachedPage() {
if($this->isCached()) {
$cachedPage = $this->getCachedPage();
@readfile($cachedPage);
exit();
}
}
public function isCached()
{
$cachedPage = $this->getChachedPage();
if (@file_exists($cachedPage)) {
$cachelast = @filemtime($cachefile);
} else {
$cachelast = 0;
}
@clearstatcache();
// Mostramos el archivo si aun no vence
if (time() - $cachetime <$cachelast) {
return true
}
else {
return false;
}
}
public function getCachedPage()
{
return $this->dir.md5($this->page).'.cache';
}
}
<?php
$page = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$frontCache = new FrontCache($page);
if($frontCache->isCached()) {
$frontCache->outputCachedPage();
}
else {
$frontCache->startCaching();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment