Ways to cache PHP variables in a file format

Source: Internet
Author: User
Tags foreach config create directory php programming

This example describes how to cache PHP variables in a file format. Share to everyone for your reference. The implementation methods are as follows:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148-149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179-18 0 181 <?php/* $cache _set = array (//cache path, finally add "/" ' CacheRoot ' => './cache/',//cache time ' CacheTime ' =>20,//cache type ' CAC Hetype ' =>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 * used to cache data, such as variables, but not cache pages/class cache{//Configure public $config = Arra Y (//cache path ' CacheRoot ' => './cache/',//cache time ' CacheTime ' =>1,//cache type 1 serialization data 2 variable ' cachetype ' =>2,//extension ' cacheexe ' = > '. php '/conversion of intermediate variables; 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__; The 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;} Circular 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-> 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; }//Cache valid return True public function cache_is ($fileName) {$fileName = $this->cache_file ($fileName); if (file_exists $f Ilename)) {//If the cache time is negative then never expires if ($this->config[' cacheTime '] < 0) {return true;}//If slowThe deposit time of 0 is always expired if ($this->config[' cacheTime '] = = 0) {return false;}//Get the cache file to be established $ctime = Intval (Filemtime ($fileName)) ; If the comparison is greater than the cache time, it is expired otherwise the period if (time ()-$ctime > $this->config[' cacheTime ']) is {return false;} else {return true;}//File not present as expiration}else {return false;}} Public Function Cache_data ($name, $data) {$varname = $name; $name = $this->cache_file ($name);//config[' CacheTime ']== 0 is to not enable caching is directly returned data if ($this->config[' cacheTime '] <> 0) {if ($this->config[' CacheType ']==1) {$write _data = "& lt;? PHP exit;? > ". Serialize ($data); return $data; }else {$write _data = "<?phprn$var="; $write _data. = Var_export ($data, true); $write _data. = ";rn?>"; $this->cache_write ($name, $write _data); return $data; }//cache filename 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_contents ($file);} else{$fopen = fopen ($file, ' R '); $cache _content = '; do {$data = Fread ($fopen, FileSize ($file)); if (strlen ($data) ===0) b Reak; $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}} Loop 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 will help you with your PHP programming.

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.