To ensure compatibility with the memory cache for which memcache is not installed on the server, a PhP5-based environment is specially developed for efficient local file caching.
It supports processing cache expiration time and saves a lot of memory. Three operations are supported: Set (), get (), del (). For detailed usage, see example in the remarks.
<? PHP/*** PHP File Cache class filecache <br/> * @ author jerryli (hzjerry@gmail.com) * @ version v0.20130513 * @ package * @ example * <PRE> * $ OFC = new cfilecache ('. /tmp/'); // create a File Cache class * $ skey =' AB _ 000000 '; // cache key value * $ DATA = $ ofC-> get ($ skey); // obtain the cache * If (is_null ($ data )) * $ ofC-> set ($ skey, array ('name' => 'ttt', 'datetime' => date ('Y-m-d h: I: s '), 10); // The cache does not exist. Create a cache * print_r ($ data); * </PRE> */final class cfilecache {/*** slow Storage directory * @ var string */Private Static $ mscachepath = NULL;/*** default cache expiration time (1 hour) * @ var int */const miexpire = 3600; /*** construct <br/> * Self: $ mscachepath the cache directory is a shared directory * @ Param string $ scachepath */function _ construct ($ scachepath = '. /tmp/') {If (is_null (SELF: $ mscachepath) Self: $ mscachepath = $ scachepath;}/*** read cache <br/> * return: cached content, string or array. If the cache is null or expires, null * @ Param string $ skey is returned. The cached key value (MD5 () is not required) * @ return string | Null * @ access public */Public Function get ($ skey) {If (empty ($ skey) return false; $ sfile = self: getfilename ($ skey ); if (! File_exists ($ sfile) return NULL; else {$ handle = fopen ($ sfile, 'rb'); If (intval (fgets ($ handle)> time ()) // check the timestamp {// No expiration time. retrieve the data $ sdata = fread ($ handle, filesize ($ sfile); fclose ($ handle ); return unserialize ($ sdata);} else {// fclose ($ handle); return NULL ;}}} /*** write cache ** @ Param string $ skey cache key value * @ Param mixed $ object to be saved by mval * @ Param int $ iexpire expiration time * @ return bool *@ access public */public function set ($ Skey, $ mval, $ 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);/* file write 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 ;}} /*** obtain the full path of the cached file <br/> * return: the full path of the cached file <br/> * $ skey value is converted to MD5 () And decomposed into three levels of directories 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 implode ('/', $ Afilename );} /*** create a directory <br/> ** @ Param string $ sdir * @ return bool */Private Static function is_mkdir ($ sdir = '') {If (empty ($ sdir) return false;/* If the cache directory cannot be created, the system will directly throw an error prompt */If (! Mkdir ($ sdir, 0666) return false; elsereturn true ;}}?>