| This article shares a php file cache data Class, which is well written in a standard way and is followed by call examples. For more information, see.
Speaking of php file caching, I reviewed the previous articles and found several articles: PHP Cache Class Call example php Data cache instance code php page caching class. for details, refer. With the above Foundation for php file caching, the following content begins today. The code is as follows:
CachePath = $ path ;}/ *** destructor */function _ destruct () {// nothing}/*** set the value of the key to $ key in the cache. if the key does not exist, create an item * @ param string $ key value * @ param mix $ var value * @ param int $ expire expiration seconds * @ param int $ flag * @ return bool if TRUE is returned if the call succeeds, otherwise, FALSE is returned. * @ Access public */public function set ($ key, $ var, $ expire = 36000, $ flag = 0) {$ value = serialize ($ var ); $ timeout = time () + $ expire; $ result = safe_file_put_contents ($ this-> cachePath. urlencode ($ key ). '. cache ', $ timeout. '<%-=-%> '. $ value); return $ result ;} /*** obtain the value of the key $ key in the cache * @ param string $ key value * @ return string if the key does not exist, returns false * @ access public */public function get ($ key ){ $ File = $ this-> cachePath. urlencode ($ key ). '. cache '; if (file_exists ($ file) {$ content = safe_file_get_contents ($ file); if ($ content = false) {return false ;} $ tmp = explode ('<%-=-%>', $ content); $ timeout = $ tmp [0]; $ value = $ tmp [1]; if (time ()> $ timeout) {$ result = false;} else {$ result = unserialize ($ value) ;}} else {$ result = false ;} return $ result;}/*** clear all items in the cache * @ return if TRUE is returned, and FALSE is returned if the task fails. * @ Access public */public function flush () {$ fileList = FileSystem: ls ($ this-> cachePath, array (), 'asc ', true); return FileSystem:: rm ($ fileList);}/*** delete the value of the $ key item in the cache. * @ param string $ key value * @ return returns TRUE if the operation succeeds, otherwise, FALSE is returned. * @ Access public */public function delete ($ key) {return FileSystem: rm ($ this-> cachePath. $ key. '. caccache') ;}} if (! Function_exists ('safe _ file_put_contents ') {function safe_file_put_contents ($ filename, $ content) {$ fp = fopen ($ filename, 'WB'); if ($ fp) {flock ($ fp, LOCK_EX); fwrite ($ fp, $ content); flock ($ fp, LOCK_UN); fclose ($ fp); return true ;} else {return false ;}} if (! Function_exists ('safe _ file_get_contents ') {function safe_file_get_contents ($ filename) {$ fp = fopen ($ filename, 'RB'); if ($ fp) {flock ($ fp, LOCK_SH); clearstatcache (); $ filesize = filesize ($ filename); if ($ filesize> 0) {$ data = fread ($ fp, $ filesize) ;}flock ($ fp, LOCK_UN); fclose ($ fp); return $ data ;}else {return false ;}}?>
Call example:
Get ('yourkey'); // yourkey is the cache name you define for each data to be cached if ($ data = false) {$ data = 'data retrieved from the database or data that is very complex and time-consuming. '; $ cache-> set ('yourkey', $ data, 3600 ); // cache 3600 seconds} // use your $ data?> |