Created
November 22, 2012 20:35
-
-
Save raul782/4132838 to your computer and use it in GitHub Desktop.
simple cache php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if(!$frontCache->isCached()) { | |
$frontCache->cachePage(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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