PHP cache code (PhP data cache)

Source: Internet
Author: User

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 "File Cache" class, hoping 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, converting array into a PHP-defined array string is written into a file. If you use include, it is equivalent to loading cache;
control the cache time by obtaining the cache file creation time and the current time. If the update time is not reached, the cache is read 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, please write one more escape character '\', that is, "\ n" must be written as "\ n ". copy Code 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 bug, you are not liable for any problems arising from the use of this Program
.
*/
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:
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 ($ ));

complex call method (with Cache Time Control) -- add it later .... To be continued...

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.