I created the caching system last night and currently in use online and its' working very smoothly.
Find the codes below:
http://www.copypastecode.com/20949/
Code:
class cache{
// Attributes
private $cache_dir; // Cache directory (Default: document_root + cache/)
private $expiry = 60; // Cache file life time in seconds
private $min_file_size = 1024; // Min file size in cache in bytes
private $file_path; // Page path / URL (Default: curent path)
// Getters and setters
public function setCacheDir($value) { $this->cache_dir = $value; }
public function setExpiry($value) { $this->expiry = $value; }
public function setMinFileSize($value) { $this->min_file_size = $value; }
public function setFilePath($value) { $this->file_path = $value; }
public function getCacheDir() { return $this->cache_dir; }
public function getExpiry() { return $this->expiry; }
public function getMinFileSize() { return $this->min_file_size; }
public function getFilePath() { return $this->file_path; }
// To be executed on object creation
public function __construct(){
// Set default cache_dir path and File path
$this->setCacheDir($_SERVER['DOCUMENT_ROOT'] . "/cache/");
$this->setFilePath(self::currentPath());
}
// Load file / Page into string
static function loadFileContent($file_path){
$file_content = file_get_contents($file_path);
return $file_content;
}
// Encryption used to decode pathes before storing them
private static function encrypt($str){
return md5($str);
}
// Method to create cache file and store it
public function createCacheFile(){
// Make sure the required attributes are defined
if($this->getCacheDir() and $this->getFilePath()){
// Gets current page output
$file_content = ob_get_contents();
// Adds when the file was cached to the page bottom
$file_content .= "";
// Encrypt file path
$file_path = $this->getCacheDir() . self::encrypt($this->getFilePath());
// Create caching file
$file_handle = fopen($file_path, 'w') or die("can't open file");
fwrite($file_handle,$file_content);
fclose($file_handle);
// Throw an exception if the required parameters aren't defined
}else{
throw new Exception(__CLASS__ . ": Please define cache_dir and file_path to continue");
}
}
// Method to load cached files
public function loadFromCache(){
// Make sure all required attributes are defined
if($this->getCacheDir() and $this->getExpiry() and $this->getMinFileSize() and $this->getFilePath()){
$file_path = $this->getCacheDir() . self::encrypt($this->getFilePath());
// If file exists gather its information (done alone to reduce memory and CPU load on file info gathering)
if(file_exists($file_path)){
// Gather file information
$file_age = time() - filemtime($file_path);
$file_size = filesize($file_path);
// If file exists and life expiry has not been reached yet and minimum file is above the minimum then include the file
if($file_age < $this->getExpiry() and $file_size > $this->getMinFileSize()){
include($file_path);
exit;
}
}
// Throw an error if the required attributes weren't defined
}else{
throw new Exception(__CLASS__ . ": Please define cache_dir, expiry, file_path and min_file_size to continue");
}
}
// Returns full current page path
static function currentPath() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
// Easy-to-implement load from cache
public static function load($expiry = null, $min_file_size = null, $file_path = null, $cache_dir = null){
// Try catch errors returned from object
try{
// Create object
$obj = __CLASS__;
$load_cache = new $obj;
// Set attributes
if($expiry !== null) $load_cache->setExpiry($expiry);
if($min_file_size !== null) $load_cache->setMinFileSize($min_file_size);
if($file_path !== null) $load_cache->setFilePath($file_path);
if($cache_dir !== null) $load_cache->setCacheDir($cache_dir);
// Load from cache
$load_cache->loadFromCache();
// Destroy instance
unset($load_cache);
}catch(Exception $cache_errors){
echo $cache_errors->getMessage();
}
}
// Easy-to-implement write / create to cache
public static function create($file_path = null, $cache_dir = null){
// Try catch errors returned from object
try{
// Create object
$obj = __CLASS__;
$create_cache = new $obj;
// Set attributes
if($file_path !== null) $create_cache->setFilePath($file_path);
if($cache_dir !== null) $create_cache->setCacheDir($cache_dir);
// Create cache file
$create_cache->createCacheFile();
// Destroy instance
unset($create_cache);
}catch(Exception $cache_errors){
echo $cache_errors->getMessage();
}
}
}
?>
It's very easy to implement:
Quote:
include('cache.class.php');
cache::create(); // Page top
cache::load(); // page bottom
|
Took me couple of hours to develop it and will consider extending it later or create a PHP based proxy server =)