Used to cache php variables as files-PHP source code

Source: Internet
Author: User
Used to cache php variables as files

1. [PHP] code
 '. /Cache/', // cache time 'cachetime' => 20, // cache type 'cachetype' => 1, // extension 'cacheexe '=> '. php '); $ cache = new Cache ($ cache_set); $ a = array ('1', '2'); $ a = "aaa "; $ B = ''; if ($ cache-> cache_is (" d ") {$ c = $ cache-> cache_read (" d "); echo" c "; print_r ($ c);} else {$ B = $ cache-> cache_data ('D', $ a);} print_r ($ B ); // $ cache-> clear ("a"); // echo $ cache-> cache_read (". /cache/d. php "); // echo $ d; * // *** data cache class v1.0 * @ author shooke * 2009-11-13 1 6:02:26 * is used to Cache data, such as variables, but cannot Cache the page */class Cache {// Configure public $ config = array (// Cache path 'cacheroot' => '. /cache/', // cache time 'cachetime' => 1, // cache type 1 stringized Data 2 variable 'cachetype' => 2, // The extension 'cacheexe '=> '. php '// Convert the intermediate variable); public $ return_name = array (); function _ construct ($ cache_set = array () {if (! Empty ($ cache_set) $ this-> config = array_merge ($ this-> config, $ cache_set ); $ this-> config ['classname'] = _ CLASS __;} public function clear ($ filename = '') {if (file_exists ($ this-> cache_file ($ filename) {@ unlink ($ this-> cache_file ($ filename);} elseif (empty ($ filename )) {$ this-> clear_dir ($ this-> config ['cacherot']);} else {$ this-> clear_dir ($ this-> config ['cacherot']. $ filename); echo $ this-> config ['cache Root ']. $ filename ;}// the path to the private function clear_dir ($ dir, $ to = false) {if ($ list = glob ($ dir. '/*') {foreach ($ list as $ file) {is_dir ($ file )? $ This-> clear_dir ($ file): unlink ($ file) ;}} if ($ to === false) rmdir ($ dir );} // write cache private function cache_write ($ filename, $ writetext, $ openmod = 'w') {if (! File_exists ($ filename) {@ $ this-> makeDir (dirname ($ filename);} if (@ $ fp = fopen ($ filename, $ openmod )) {flock ($ fp, 2); fwrite ($ fp, $ writetext); fclose ($ fp); return true;} else {echo "File: $ filename write error. "; return false ;}// return true public function cache_is ($ fileName) {$ fileName = $ this-> cache_file ($ fileName ); if (file_exists ($ fileName) {// if the cache time is negative, it never expires if ($ this-> config ['cachetime'] <0) {return true ;} // if the cache time is 0, it will always expire if ($ this-> config ['cachetime'] = 0) {return false ;} // Obtain the cache file creation time $ ctime = intval (filemtime ($ fileName); // compare whether the cache time is greater than the cache time; if yes, it expires; otherwise, it does not expire if (time () -$ ctime> $ this-> config ['cachetime']) {return false;} else {return true ;} // if the file does not exist, it is deemed that it has expired.} else {return false;} public function cache_data ($ name, $ data) {$ varname = $ name; $ name = $ this-> cache_file ($ name ); // config ['cachetime'] = 0. if cache is not enabled, data is directly returned if ($ this-> config ['cachetime'] <> 0) {if ($ this-> config ['cachetype '] = 1) {$ write_data ="
 ". Serialize ($ data); // return $ data;} else {$ write_data ="
 ";}$ This-> cache_write ($ name, $ write_data) ;}return $ data ;}// cache file name private function cache_file ($ filename) {return $ this-> config ['cacherot']. $ filename. $ this-> config ['cacheexe '];} // read the public function cache_read ($ file) {$ file = $ this-> cache_file ($ file); if (! File_exists ($ file) {return '';} if ($ this-> config ['cachetype '] = 1) {if (function_exists ('File _ get_contents ')) {$ cache_Content = file_get_contents ($ file);} else {$ fopen = fopen ($ file, 'r'); $ cache_Content = ''; do {$ data = fread ($ fopen, filesize ($ file); if (strlen ($ data) = 0) break; $ cache_Content. = $ data;} while (1); fclose ($ fopen);} $ cache_Content = substr ($ cache_Content, 13);/* Remove
 */$ Cache_Content = unserialize ($ cache_Content); return $ cache_Content;} else {include_once ($ file); return $ var ;}} // cyclically create the directory private function makeDir ($ dir, $ mode = 0777) {if (! $ Dir) return 0; $ dir = str_replace ("\\\\", "/", $ dir); $ mdir = ""; foreach (explode ("/", $ dir) as $ val) {$ mdir. = $ val. "/"; if ($ val = ".. "| $ val = ". "| trim ($ val) =" ") continue; if (! File_exists ($ mdir) {if (! @ Mkdir ($ mdir, $ mode) {return false ;}} return true ;}}?>

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.