By checking
if the traffic is large, it will cause a great burden on the database, so for content that changes infrequently, PHP data cache (caching) must be done ) is very necessary. I made a simple PHP data caching class. I hope it will be helpful to everyone.
The idea is as follows:
For general variables, change the variable into the PHP language format and write it to the file. Just include this file, which is equivalent to loading the PHP data cache. class;
For array-type variables, convert the array into a string that defines the array in PHP language, and write it to the file. It only takes the time to include, which is equivalent to loading the cache;
PHP data cache class time control is achieved by obtaining the creation time of the cache file and comparing it with the current time. If the update time is not reached, the cache is read directly. If the update time is reached, the database is queried, the data is returned, and the cache is updated. (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".
- /*
-
//PHP-kcache class v_0.1
- //Author: kangzj
- //Email : kangzj@mail.bnu.edu.cn
- //Blog : http://kangzj.net.ru
- //作者不保证本程序没有bug,对于使用本程序
- //而引起的任何问题不担负任何责任。
- */
-
class PHP_kcache {
- //相对或者绝对目录,末尾不要加 '/'
-
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;
- }
- }
- //将array变成字符串, 来自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;
- }
- }
最简单的PHP数据缓存类调用方法:
<ol class="dp-xml">
<li class="alt"><span><span>include './PHP_kcache_class.PHP'; </span></span></li>
<li>
<span>$</span><span class="attribute">pc</span><span> = </span><span class="attribute-value">new</span><span> PHP_kcache; </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">a</span><span> = </span><span class="attribute-value">array</span><span>('a', 'b', 'c'); </span>
</li>
<li>
<span>$pc-</span><span class="tag">></span><span>set_cache('a', addslashes($a)); </span>
</li>
</ol>
http://www.bkjia.com/PHPjc/445945.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445945.htmlTechArticleEveryone knows that if the number of visits is large, it will cause a great burden on the database, so for content that changes infrequently It is very necessary to do a good PHP data cache (caching). I made one...
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