How does PHP have no application cache like ASP? Recently I have been looking for a lot, all only own write, below I share a piece of code
classphp_cache{//relative or absolute directory, do not add '/' at the end var $cache _dir= './cache '; var $cache _extension= '. cache.php '; functionSet_cache ($name,$value){ $pre= "< \n//cache Created at:".Date(' y-m-d h:i:s '). " \ n "; if(!Is_array($value)){ $value=$value; $str= "\$$name=$value‘;"; }Else{ $str= "\$$name= " .$this->arrayeval ($value) . ‘;‘; } $end= "\n?>"; Echo $cache=$pre.$str.$end; $cache _file=$this->cache_dir. ‘/‘ .$name.$this-cache_extension; if($fp= @fopen($cache _file, ' WB ')) { fwrite($fp,$cache); fclose($fp); return true; } Else { Echo $cache _file; Exit(' Can not be write to cache files, please check cache directory '); return false; } } //converts an array into a string from discuz! functionArrayeval ($array,$level= 0) { if(!Is_array($array)) { return"‘".$array."‘"; } $space= ' '; for($i= 0;$i< =$level;$i++) { $space. = "\ T"; } $evaluate= "array\n$space(\ n "; $comma=$space; if(Is_array($array)) { foreach($array as $key=$val) { $key=is_string($key) ? ‘\‘‘.addcslashes($key, ‘\‘\\‘).‘ \‘‘ :$key; $val= !Is_array($val) && (!Preg_match("/^\-? [1-9]\d*$/],$val) ||strlen($val) > 12)? ‘\‘‘.addcslashes($val, ‘\‘\\‘).‘ \‘‘ :$val; if(Is_array($val)) { $evaluate.= "$comma $key= ". Arrayeval ($val,$level+ 1); } Else { $evaluate.= "$comma $key=$val"; } $comma= ", \ n$space"; } } $evaluate. = "\ n$space)"; return $evaluate; }}
The calling code is as follows:
include './phpcache_class.php '; $pc New Php_cache; $a Array (' A ', ' B ', ' C '); $pc addslashes ($a));
PHP application cache code sharing with ASP