PHP Cache Class
$oFC = new Filecache ();
$sKey = ' ab_123 ';
$data = $oFC-> get ($sKey);
if (Is_null ($data))
$oFC-> Set ($sKey, Array (' name ' => ' TTT ', ' DateTime ' => date (' y-m-d h:i:s ')), 100000;
Print_r ($data);
<?php Final class Filecache {/** * * * Cache Directory * * * @var string/private static $MSCAC
Hepath = null;
/** * * * * Default cache expiration Time (1 hours) * * * @var int/Const MIEXPIRE = 3600; /** * * * <br/> * Self:: $msCachePath cache directory for shared directory * * * * @param string $sCachePath * * * funct Ion __construct ($sCachePath = './tmp/') {if (Is_null (self:: $msCachePath)) Self:: $msCachePath
= $sCachePath; /** * * * * * Read cache <br/> * Return: Cached content, string or array, cache empty or expired return NULL * * * @param string $sKey cache key value (no MD5 ()) * * @return String NULL * * @access public/Public function get ($sKey) {if (empty) ($sK
EY)) return false;
$sFile = self:: GetFileName ($sKey);
if (!file_exists ($sFile)) return null;
else {$handle = fopen ($sFile, ' RB ');
if (Intval (fgets ($handle)) > Time ()) {//Check timestamp {//No expiration period, remove data $sData = Fread ($handle, FileSize ($sFile));
Fclose ($handle);
Return Unserialize ($sData);
else {//the expiration period fclose ($handle);
return null;
/** * Write Cache * * * * @param string $sKey Cache key Value * * @param mixed $mVal objects to be saved * * @param int $iExpire failure time * @return BOOL * @access public/Public function set ($sKey, $mVa
L, $iExpire = null) {if (empty ($sKey)) return false;
$sFile = self:: GetFileName ($sKey);
if (!file_exists (DirName ($sFile))) if (!self:: Is_mkdir (DirName ($sFile))) return false;
$aBuf = Array ();
$aBuf [] = time () + ((Empty ($iExpire))? Self:: Miexpire:intval ($iExpire));
$aBuf [] = serialize ($mVal);
/** * Write File Operation * * $handle = fopen ($sFile, ' WB ');
Fwrite ($handle, implode ("\ n", $aBuf));
Fclose ($handle);
return true;
/** * * * * Delete the specified cache key value * * * @param string $sKey cache key value * @return bool
* * Public Function del ($sKey) {if (empty ($sKey)) return false;
else {@unlink (self:: GetFileName ($sKey));
return true; }/** * * * Get cached file full path <br/> * Return: Cached file full path <br/> * $sKey value will be converted to MD5 () and decomposed into Level 3 directory for access
* * * @param string $sKey Cache Key * * @return String * @access protected * *
private static function GetFileName ($sKey) {if (empty ($sKey)) return false;
$key _MD5 = MD5 ($sKey);
/** * $aFileName = array (); * $aFileName [] = RTrim (self:: $msCachePath, '/'); * $aFileName [] = $key _md5{0}.
$key _md5{1}; * $aFileName [] = $key _md5{2}.
$key _md5{3}; * $aFileName [] = $key _md5{4}.
$key _md5{5};
* $aFileName [] = $key _md5; */return Self:: $msCachePath. '/' .
$key _md5;
Return implode ('/', $aFileName);
/** * * * * * Create directory <br/> * * * @param string $sDir * @return bool * * private static function is_mkdir ($sDir = ') {if (empty ($sDir)) ret
Urn false;
/** * If the cache directory cannot be created, let the system throw the error message directly/echo $sDir;
if (!mkdir ($sDir, 0666)) return false;
else return true;
}
}