Home >Backend Development >PHP Tutorial >php cache class code php data cache class

php cache class code php data cache class

WBOY
WBOYOriginal
2016-07-29 08:42:221036browse

If the number of visits is large, it will put a lot of burden on the database, so it is very necessary to do a good PHP data cache for content that changes infrequently. I made a simple PHP "file cache" class. I hope Helpful to everyone.
The idea is like this:
For general variables, change the variable into the format of PHP language and write it to the file. Just include this file is equivalent to loading the cache;
For array-type variables, convert the array Define the array string for the PHP language and write it to the file. It only takes the time to include it, which is equivalent to loading the cache;
Cache cache time control, by getting the creation time of the cache file and comparing it with the current time. If there is no When the update time comes, read the cache directly. If the update time comes, query the database, return the data, and then update the cache. (Not yet implemented)
The following is my php-kcache class (php_kcache_class.php):
Note: If it is a cache string, please add one more '' to the escape character, that is, "n" should be written as "\n" .

Copy the 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 is bug-free and is not responsible for any problems caused by using 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 = $value;
              $str = "$$name = '$value';";
            }else{
                      $str = "$$name = " . $this->arrayeval($value) . '; ';
} $ $ end = "n? & Gt;";
echo $ cache = $ Pre. $ Str. $ End;
$ cache_file = $ this- & gt; cache_dir.'. & gt; cache_extent;
if ($ fp = @fopen ($ cache_file, 'wb')) {
fwrite ($ fp, $ cache);
fclose ($ fp); {O Echo $ cache_file;
                                                                                                                           . , $level = 0) {(if (! IS_ARAY ($ array)) {
Return "'". $ Array. "'";
}
$ space = ''; $level; $i++) {
  
foreach( $array as $key => $val) {
                                                                                                                                                                                                                                          $array                  $val ($val) && (!preg_match("/^-?[1-9]d*$/", $val) || strlen($val) > 12) ? '''.addcslashes($val, ' '\').''' : $val;
                                                                                                                                                                   } else {
                    $evaluate .= "$comma$key =>
         }
          $evaluate .= "n$space)";
            return $evaluate;
}
}


The simplest calling method:



Copy the code

The 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 calling method (plus cache time control) - will be added later… to be continued…

The above introduces the php cache class code and php data cache class, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn