php页面缓存
<div id="cnblogs_post_body"> 这几天接触了phpcms的页面缓存,有些感触。其好处就不多说了,它一般是用在数据库查询较多的页面中,对于插入修改删除的页面就不大合适了。这里有缓存技术的简单介绍:http://www.cnblogs.com/penghcn/articles/2720202.html
php页面缓存主要用到的是ob系列函数,如ob_start(),ob_end_flush(),ob_get_contents()
下面是编码部分。
1.初始化函数,一般是设置页面缓存路径、缓存文件命名格式等,可按个人喜好自定义。这里用到的识别ID是经加密的$_SERVER参数。这个函数中最后还有一个if判断:若未过缓存期,则加载缓存文件,否则加载源文件。
<div class="cnblogs_code"> 1 function page_init() 2 { 3 $url = $_SERVER['REQUEST_URI'];//子url,该参数一般是唯一的 4 $pageid = md5($url); 5 $dir = str_replace('/','_',substr($_SERVER['SCRIPT_NAME'],1,-4)); 6 //目录命名方式,如exp_index 7 if(!file_exists($pd = PAGE_PATH.$dir.'/'))@mkdir($pd,0777) or die("$pd目录创建失败"); 8 //如cache/page/exp_index/ 9 define('PAGE_FILE',$pd.$pageid.'.html');10 //如cache/page/exp_index/cc8ef22b405566745ed21305dd248f0e.html11 $contents = file_get_contents(PAGE_FILE);//读出12 13 if($contents && substr($contents, 13, 10) > time() )//对应page_cache()函数中加上的自定义头部14 {15 echo substr($contents, 27);16 exit(0);17 }18 return true; 19 }
页:
[1]