Created
August 13, 2010 17:54
-
-
Save mcgivrer/523269 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
/** | |
* Cache manager. | |
* if a cache file named following url'ification rules already exists, | |
* the file is serve in place of generated one with PHP scripting. | |
* Files are store into a default cache path "../cache/" relatively to the root | |
* of web site (htdocs, in apache world). | |
* | |
* Extracted from http://github.com/mcgivrer/Games project | |
* | |
* @author Frédéric Delorme<[email protected]> | |
* @version 1.3 | |
* @copyright 2010/08/08 | |
*/ | |
class Cache{ | |
static private $_instance = null; | |
private $filters = array('include'=>array(),'exclude'=>array()); | |
private $cachePath =""; | |
public function Cache(){ | |
//echo "<pre>Cache</pre>"; | |
} | |
/** | |
* Default constructor for Cache manager. | |
* if $pCachePath is provided, take it in account. | |
* if no cachePath is define, a default one is set. | |
* @param string $pCachePath default path for cache files. | |
*/ | |
public function __construct($pCachePath=""){ | |
$this->cachePath = ($pCachePath!=""?$pCachePath:($this->cachePath!=""?$this->cachePath:dirname(__FILE__)."/../cache/")); | |
} | |
public function addIncludeFilter($filter){ | |
$this->filters['include'][$filter]=$filter; | |
} | |
public function addExcludeFilter($filter){ | |
$this->filters['exclude'][$filter]=$filter; | |
} | |
public function notFiltered(){ | |
if(isset($_REQUEST['nocache'])){ | |
//echo "<pre>nocache</pre>"; | |
return false; | |
} | |
if(isset($_REQUEST['forcerefresh'])){ | |
//echo "<pre>forcerefresh</pre>"; | |
return true; | |
} | |
foreach($this->filters['exclude'] as $filter){ | |
if($filter!="" && strstr($_SERVER['QUERY_STRING'],$filter)){ | |
//echo "<pre>exclude filter</pre>"; | |
return false; | |
} | |
} | |
foreach($this->filters['include'] as $filter){ | |
if($filter =="*" || (isset($filter) && $filter!="" && strstr($_SERVER['QUERY_STRING'],$filter))){ | |
//echo "<pre>include filter</pre>"; | |
return true; | |
} | |
} | |
return true; | |
} | |
public function isExists(){ | |
if($this->notFiltered()){ | |
//echo "<pre>search: ".HtmlTools::encodeUrlParam($_SERVER['QUERY_STRING'])."</pre>"; | |
return file_exists($this->cachePath.HtmlTools::encodeUrlParam($_SERVER['QUERY_STRING']).".cache"); | |
}else{ | |
return false; | |
} | |
} | |
public function start(){ | |
if($this->notFiltered()){ | |
ob_start(); | |
} | |
} | |
public function flush(){ | |
if($this->notFiltered() || error_get_last()!=null){ | |
$content= ob_get_contents(); | |
ob_end_clean(); | |
$file=fopen($this->cachePath.HtmlTools::encodeUrlParam($_SERVER['QUERY_STRING']).".cache","w+"); | |
fputs($file,$content); | |
fclose($file); | |
echo $content; | |
}else{ | |
echo "<pre>page cache filtered</pre>"; | |
} | |
} | |
public function getCachedPage(){ | |
$content=file_get_contents($this->cachePath.HtmlTools::encodeUrlParam($_SERVER['QUERY_STRING']).".cache"); | |
echo $content; | |
} | |
public static function getInstance($cachePath=""){ | |
if(!isset(self::$_instance)){ | |
self::$_instance=new Cache($cachePath); | |
} | |
return self::$_instance; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just a simple Cache Manager for small PHP page generator. Based on $_SERVER['QUERY_STRING'] to store cache value.
Very usefull for Catalog site, or presentation site. Generated page are stored into a "cache/" directory.
how to use:
Have fun ! :D