PHP的文件缓存机制

PHP的文件缓存机制

 

使用缓存文件来减轻频繁对数据库访问的压力,当然这些数据一般是不怎么改动的,比如一些配置信息。

 

用来生成与更新缓存文件的

qinmi/func/cache_file.php

 

/*生成缓存文件*/

function build_cache_file() {

       $data = array();

       $data['k1'] = 'v1';

       $data['k2'] = 'v2';

       $data['k_arr'] = array('a','b','c',array(1,2,3));

       $data['domain'] = array('rczjp.cn','baidu.com','google.com.hk');

       writetocache('file', getcachevars(array('file' => $data)));

}

 

 

缓存文件

qinmi/cache/cache_file.php

 

写入与更新缓存的函数

//--------------更新写入缓存文件------------//

function updatecache($cachename = '') {

$updatelist = empty($cachename) ? array() : (is_array($cachename) ? $cachename : array($cachename));

       foreach($updatelist as $entry) {

              include_once "./qinmi/func/cache_$entry.php";

              call_user_func('build_cache_'.$entry);

       }

}

 

function writetocache($script, $cachedata, $prefix = 'cache_') {

       $dir = './qinmi/cache/';

       if(!is_dir($dir)) {

              mkdir($dir, 0777);

       }

       if($fp = @fopen("$dir$prefix$script.php", 'wb')) {

              $edit_date = date('Y-m-d h:i:s A',time())." ".date('l');

              fwrite($fp, "<?php \n /* \n  以下是程序自动生成的缓存文件,请勿修改! \n 修改日期:$edit_date \n 秦迷空间:http://www.rczjp.cn \n */ \n $cachedata ?>");

              fclose($fp);

       } else {

              exit('不能写入,请检查您的cache目录权限');

       }

}

//------------------------------------------//

 

//----------------格式化(变量与数组格式)------------------//

function getcachevars($data, $type = 'VAR') {

       $evaluate = '';

       foreach($data as $key => $val) {

              if(!preg_match("/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/", $key)) {

                     continue;

              }

              if(is_array($val)) {

                     $evaluate .= "\$$key = ".arrayeval($val).";\n";

              } else {

                     $val = addcslashes($val, '\'\\');

                     $evaluate .= $type == 'VAR' ? "\$$key = '$val';\n" : "define('".strtoupper($key)."', '$val');\n";

              }

       }

       return $evaluate;

}

function arrayeval($array, $level = 0) {

       if(!is_array($array)) {

              return "'".$array."'";

       }

       if(is_array($array) && function_exists('var_export')) {

              return var_export($array, true);

       }

 

       $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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值