PHP Cache Class Code (PHP data cache Class) _php tutorial

Source: Internet
Author: User
If the traffic is large, it will be a huge burden on the database, so it is very necessary to do the PHP data cache (cache) for the content of the change infrequently, I have made a simple php "file cache" class, I hope to be helpful to everyone.

The idea is this:

For the general variable, the variable into the PHP language format, write to the file, as long as the include this file is equivalent to loading the cache;
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;
Cached cache 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 ' \ ', or ' \ n ' to be written as "\\n".
Copy CodeThe 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 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, 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 = "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 method of invocation:
Copy CodeThe code is as follows:
Include './php_kcache_class.php ';
$pc = new Php_kcache;
$a = Array (' A ', ' B ', ' C ');
$pc->set_cache (' A ', addslashes ($a));

Complex invocation methods (plus cache time Control)--later on ... to be continued ...

http://www.bkjia.com/PHPjc/321536.html www.bkjia.com true http://www.bkjia.com/PHPjc/321536.html techarticle 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 (caching) for the content of the change infrequently, I made a simple php "article ...

  • 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.