源自:http://www.trindo.cn/show-48.html
Submitted by shinian315 on 2009, August 26, 10:24 AM. PHP
一直都在使用discuz与supesite的缓存机制,大致机制清楚,但是没有系统的分析过 ,今天有空分析了下代码。主要是包含存放模板缓存文件的 forumdata文件夹 include下的template.func.php、 global.func.php文件,主要是用到 这2个文件中的一些函数。再有就是存放模板文件的templates文件夹。我首先 新建了一个index.php文件。
PHP代码
- <?php header(“Content-type=text/html; charset=utf- 8”); require_once ‘./common.inc.php’; $timestamp = time(); $tplrefresh = 1; //控制当模板改变时立刻更新缓存 $header = “This is header”; $main = “This is main”; $footer = “This is footer”; include template(“index”); ?>
顺 便把common.inc.php的文件也粘出来
PHP代码
- <?php define(‘IN_TRINDO’,true); define(‘TRINDO_ROOT’,dirname(__FILE__).DIRECTORY_SEPARATOR); define(‘TEMPLATEID’, ‘1’); define(‘TPLDIR’,’./templates/default’); require_once ‘./include/global.func.php’; ? >
看index.php中的include template(“index”),主要就是调用template函数。那我们接着看include下的 global.func.php里的template函数(下面的代码每个empty前多出了一个empty,编辑器的 问题)
PHP代码
- <?php function strexists($haystack, $needle) { return !(strpos($haystack, $needle) === FALSE); } function checktplrefresh($maintpl, $subtpl, $timecompare, $templateid, $tpldir) { global $tplrefresh; echo “缓存文件 最后修改时间:”.date (“Y-m- d H:i:s”,$timecompare).”<br>”; echo “模板文件 最后修改时间:”.date (“Y-m- d H:i:s”,filemtime($subtpl)).”<br>”; echo “现在时间 :”.date(“Y-m-d H:i:s”,time()).”<br><br>”; if(emptyempty($timecompare) || $tplrefresh == 1 || ($tplrefresh > 1 && ! ($GLOBALS[‘timestamp’] % $tplrefresh))) { if(emptyempty($timecompare) || @filemtime($subtpl) > $timecompare) { &nbs p; require_once TRINDO_ROOT.’./include/template.func.php’; & nbsp; parse_template($maintpl, $templateid, $tpldir); &nbs p; return TRUE; } } return FALSE; } function template($file, $templateid = 0, $tpldir = ”) { global $inajax; $file .= $inajax && ($file == ‘header’ || $file == ‘footer’) ? ‘_ajax’ : ”; $tpldir = $tpldir ? $tpldir : TPLDIR; $templateid = $templateid ? $templateid : TEMPLATEID; $tplfile = TRINDO_ROOT.’./’.$tpldir.’/’.$file.’.htm’; $objfile = TRINDO_ROOT.’./forumdata/templates/’.$templateid.’_’.$file.’.tpl.php’; if($templateid != 1 && ! file_exists($tplfile)) { $tplfile = TRINDO_ROOT.’./templates/default/’.$file.’.htm’; } @checktplrefresh($tplfile, $tplfile, filemtime($objfile), $templateid, $tpldir); return $objfile; } ? >
这里$tplfile为模板文件, $objfile为模板缓存文件,$tpldir为模板存放文件夹,这里默认的是/templates/default 。主要来看checktplrefresh这个函数,我在这个函数里标记了几个文件时间,以便更容易 的分析。$tplrefresh是一旦模板有改动是否立刻重新生成缓存的控制开关,我这里设为1 ,开启了。$timecompare为模板缓存的最后修改的时间,如果第一次运行没有生产缓存的 话,其为空,那我们就得引用template.func.php文件里的parse_template函数了,这个函 数的主要用途就是利用正则将模板文件里的标签替换成php语法,然后写入到模板缓存文件 中,成功后template就将一个已经生产的模板缓存文件返回,这个再include的话就直接可 以输出了。如果第一次生成缓存成功的话,下次就不会再执行parse_template 函数去重新编译模板了,直接就将已存在的缓存文件返回,这样就提高了效率。一旦模板文件有改动,则@filemtime($subtpl) > $timecompare成立,系 统将会重新执行parse_template函数去编译模板,修改的效果就立刻生效了。 第一个青春是上帝给的;第二个的青春是靠自己努力的
相关文章:
你感兴趣的文章:
- 网友感叹:Linux光盘在市场上消失?
- PHOTOSHOP入门笔刷介绍
- 小米路由器Pro设置教程:详细指南和无线路由器设置方法
- 复数阻抗Z
- 安卓手机当U盘使用拷贝数据资料的操作步骤
- 机械加工工艺系统的原始误差