A way to cache PHP variables as a file,
The example in this article describes how to cache php variables in file form. Share to everyone for your reference. The implementation method is as follows:
<?php/* $cache _set = Array (//cache path, finally add "/" ' cacheroot ' = './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 16:02:26 * For caching data, such as variables, but cannot cache pages */class cache{//Configure public $config = Array (//cache path ' CacheRoot ' =&G t; '. /cache/',//cache time ' CacheTime ' =>1,//cache type 1 serialization data 2 variables ' cachetype ' =>2,//extension ' cacheexe ' = '. php '//convert 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[' cacheroot ']); }else{$this->clear_dir ($this->config[' CacheRoot '). $filename); echo $this->config[' CacheRoot '). $filename; }}//loop delete path 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->m Akedir (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; }}//cache validity valid returns true Public function Cache_is ($fileName) {$fileName = $this->cache_file ($fileName); if (file_exists ($fileName)) {//If the cache time is negative then never expire if ($this->config[' cacheTime ') <0) {return true; }//If the cache time is 0, it expires if ($this->config[' cacheTime ') = = 0) {return false; }//Gets the settling time of the cache file $ctime = intval (Filemtime ($fileName)); If the comparison is greater than the cache time, it is expired or if (time ()-$ctime > $this->config[' cacheTime ') {return false; }else {return true; }//file does not exist as expired invalid}else {return false; }} Public Function Cache_data ($name, $data) {$varname = $name; $name = $this->cache_file ($name); config[' CacheTime ']==0 that is, do not enable caching is the direct return data if ($this->config[' cacheTime '] <> 0) {if ($this->config[' CacheType ']==1) {$write _data = "<?php exit;? > ". Serialize ($data); return $data; }else {$write _data = "<?php\\r\\n\\ $var ="; $write _data. = Var_export ($data, true); $write _data. = ";\\r\\n?>"; } $this->cache_write ($name, $write _data); } return $data; }//cache file name Private function Cache_file ($filename) {return $this->config[' CacheRoot '). $filename. $this->config[' Cacheexe ']; }//Read file 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_content S ($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 <?php exit; > * * $cache _content = unserialize ($cache _content); return $cache _content; }else{include_once ($file); return $var; }}//Looping Create 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; }}?>
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/1022785.html www.bkjia.com true http://www.bkjia.com/PHPjc/1022785.html techarticle to cache PHP variables as a file, this article describes how to cache php variables in file form. Share to everyone for your reference. The concrete implementation method is as follows: php/* $cache _s ...