Everyone through the
If the traffic is large, it will be a huge burden on the database, so it is very necessary to do a good job of PHP data cache (cache) for the content of infrequently changing, I have made a simple PHP data caching class, I hope to help you.
The idea is this:
For the general variable, the variable into the PHP language format, write to the file, the time as long as the include this file is equivalent to loading the PHP data cache class;
For the variant of the array type, the array is converted to the PHP language definition array string, written to the file, as long as the include is equivalent to loading the cache;
PHP Data cache class time control, by obtaining the cache file creation time and the current time to compare, if not to update time, directly read the cache, if the update time, query the database, return data, and then update the cache. (not yet implemented)
Here is my Php-kcache class (Php_kcache_class. PHP):
Note: If the string is cached, write the escape character one more ", that is," n "to be written as" n ".
- /*
- // 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 does not have a bug, for the use of this program
- Without any responsibility for any problems arising.
- */
- 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;
- }
- }
- Converts an 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 = "Arrayn$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 method to invoke the PHP data cache class:
- Include './php_kcache_class. PHP ';
- $ PC new php_kcache;
- $ a Array(' A ', ' B ', ' C ');
- $pc- > Set_cache (' A ', addslashes ($a));
http://www.bkjia.com/PHPjc/445945.html www.bkjia.com true http://www.bkjia.com/PHPjc/445945.html techarticle everyone through to if the volume of access to the database will be a great burden, so for the change of the content of the PHP data cache is very necessary, I made a ...