PHP Abstract Cache Type

Source: Internet
Author: User
Tags flock
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 ());
*/
?>
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.