PHP Abstract Cache Class
Require_once dirname (__file__). " /.. /model/iface/icache.php ";
Abstract class Allcache implements Icache
{
Static $cache _dir;
Const CACHETIME=60;//Cache time is 60 seconds
Static $_data;
Static $_interce;
constructor function
Public Function __construct ()
{
$cache _dirname = dirname (__file__). " /.. /cache ";
if ([email protected]_dir ($cache _dirname))
{
if ([email protected] ($cache _dirname,0777))
{
Self::msgbox (' cache file does not exist and cannot be created, need to be created manually. ');
return false;
}
}
Self:: $cache _dir = $cache _dirname;
}
/**
* Static Call
* Self refers to the class itself
*/
public static function getinstance ()
{
if (! (Self::$_interce instanceof Self)) {
Self::$_interce = new self ();
}
return self::$_interce;
}
Output exception
Private Function MsgBox ($errorstring)
{
echo "
error occurred:". $errorstring."
";
}
Set the cache file
Private Function Cache_page ($pageurl, $pagedata)
{
if (! $fso =fopen ($pageurl, ' W ')) {
Self::msgboxs (' cannot open cache file. '); /trigger_error
return false;
}
if (!flock ($fso, lock_ex)) {//LOCK_NB, exclusive type lock
Self::msgboxs (' cannot lock cache file. '); /trigger_error
return false;
}
if (!fwrite ($fso, $pagedata)) {//write byte stream, serialize write to other format
Self::msgboxs (' cannot write cache file. '); /trigger_error
return false;
}
Flock ($fso, lock_un);//Release lock
Fclose ($FSO);
return true;
}
Read file status
Private Function Isopencache ($cacheFile)
{
if (!file_exists ($cacheFile)) {
Self::msgbox (' cannot read cache file. '); Exit
}
/*echo ' Read cache file: '. $cacheFile;
Return Unserialize (file_get_contents ($cacheFile));
$fso = fopen ($cacheFile, ' R ');
$data = Fread ($fso, FileSize ($cacheFile));
Fclose ($FSO);
return $data; */
}
/**
* Formatted data
*/
Private Function Setfilecontent ($data)
{
$html = " $html. = "\ $data =". Var_export ($data, true). "; n ";
$html. = "?>";
return $html;
}
/**
* Set Cache data
* @param $cacheFile Cache file name
X @param $_data Cache data Source
*/
Public Function readdata ($cacheFile = ' default_cache.txt ', $_data)
{
$_data = Self::setfilecontent ($_data);
$cacheFile = self:: $cache _dir. " /". $cacheFile;
if (file_exists ($cacheFile))
{
Self::isopencache ($cacheFile);
if (Filemtime ($cacheFile) < (time ()-self::cachetime))
{
Self::cache_page ($cacheFile, $_data);
}
}else{
Self::cache_page ($cacheFile, $_data);
}
Require_once ($cacheFile);
Self::$_data = $data;
}
/**
* Get Data
*/
Public Function GetData ()
{
return self::$_data;
}
}
/*
Allcache::readdata ("cache_news.php", Array (0,1,2,3,5,6));
Print_r (Allcache::getdata ());
*/
?>