Php Data cache code ). If the traffic volume is large, it will cause a great burden on the database. Therefore, it is necessary to cache php Data for infrequently changed content, I made a simple php: "If there is a large amount of traffic, it will cause a great burden on the database. Therefore, we need to cache php Data for infrequently changed content) it is very necessary. I made a simple php "file cache" class and hope to help you.
The idea is as follows:
For a common variable, convert the variable to the php format and write it to the file. if you include this file, it is equivalent to loading the cache;
For array-type variables, convert the array into a php-defined array string and write it to the file. if you use include, it is equivalent to loading the cache;
Cache time control: compare the cache file creation time with the current time by obtaining the cache file creation time. if the update time is not reached, read the cache directly. if the update time is reached, query the database, return data and update the cache. (Not implemented yet)
Below is my php-kcache class (php_kcache_class.php ):
Note: If it is a cache string, write one more escape character '\', that is, "\ n" to be written as "\ n ".
The code is as follows:
/*
// Php-kcache class v_0.1
// Author: kangzj
// Email: kangzj@mail.bnu.edu.cn
// Blog: http://kangzj.net.ru
// The author does not guarantee that this program has no bugs.
// You are not liable for any problems arising therefrom.
*/
Class php_kcache {
// Relative or absolute directory, do not add '/' at the end '/'
Var $ cache_dir = './cache ';
Var $ cache_extension = '. cache. php ';
Function set_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 write to cache files, please check cache directory ');
Return false;
}
}
// Convert array into a string from discuz!
Function arrayeval ($ 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 simplest call method:
The code is as follows:
Include './php_kcache_class.php ';
$ Pc = new php_kcache;
$ A = array ('A', 'B', 'C ');
$ Pc-> set_cache ('A', addslashes ($ ));
Complex call methods (with cache time control) -- add it later .... To be continued...
Caching data cache is very necessary. I made a simple php "text...