建站软件 优化软件 编程软件 网页辅助 站群程序 网站程序 图像处理 资源教程 字体下载 推荐软件


您的位置:首页 > 建站必知 > phpcms缓存使用总结

phpcms缓存使用总结

时间:2015-07-08 11:09:14  来源:免费模板网 作者:风雪 阅读次数 tags缓存phpcms

缓存可以加快网站访问速度,减轻服务器压力。主流的cms都采用了缓存机制,那么phpcms怎么实现缓存的呢
a.模板编译缓存

参考文件include/global.func.php及include/template.func.php
模板编译缓存的原理其实很简单,如果模板是第一次编译,则直接编译它,如果不是第一次编译,则比较模板文件($tplfile)及模板缓存文件 ($compiledtplfile)的修改时间,如果模板文件的修改时间大于编译过的模板缓存文件,则编译模板,否则不编译模板,提高了程序的执行效 率。

 

 
代码如下:

function template($module = 'phpcms', $template = 'index')
{
global $CONFIG;
$compiledtplfile = $CONFIG['templatescachedir'].$module.'_'.$template.'.tpl.php';
if($CONFIG['templaterefresh'])
{
$tplfile = PHPCMS_ROOT.'/templates/'.$CONFIG['defaulttemplate'].'/'.$module.'/'.$template.'.html';
if(!file_exists($compiledtplfile) || @filemtime($tplfile) > @filemtime($compiledtplfile))
{
require_once PHPCMS_ROOT.'/include/template.func.php';
template_refresh($tplfile, $compiledtplfile);
}
}
return $compiledtplfile;
}

 

b.在动态页面里面产生静态的缓存文件

与c的缓存原理类似,只是此处生成的文件名相对固定
以问吧模块为例进行说明
用http://www.chf.com/opensource/phpcms2007_sp6_gbk/phpcms/wenba/进行访问
此目录下有个index.php文件,判断当前目录下是否存在名为index_cache.html的文件,如果有没有过失效期,则直接包含此文件,否则动态地读取完数据后保存为index_cache.html文件,以备下次使用。
文件index.php中的内容:

 

 
代码如下:

<?php
require_once './include/common.inc.php';
$lastedittime = @filemtime('index_cache.html');
$lastedittime = $PHP_TIME-$lastedittime;
$autoupdatetime = intval($MOD['autoupdate']); //$MOD['autoupdate']来自缓存文件data/cache/wenba_setting.php中的内容
if(file_exists('index_cache.html') && $lastedittime<$autoupdatetime)

echo "include cache file";
include 'index_cache.html';
}
else
{
echo "read dynamic page";
...
?>

 

怎么判断文件是否失效呢,文件data/cache/wenba_setting.php中有如下的设置,其中字段autoupdate的值就是文件失效的时间,单位是秒,在后台可以进行设置
文件wenba_setting.php是从哪儿来的呢,进行安装时自动把各种模块的数据保存到数据库中了,安装时就生成缓存数据了,在include/common.inc.php中函数cache_all也可以生成缓存,后台进行设置时cache会自动更新的

 

 
代码如下:

<?php
return array (
'higth_score' => '100',
'anybody_score' => '2',
'answer_give_credit' => '5',
'vote_give_credit' => '1',
'highscore' => '2',
'vote_give_actor' => '公司白领</p> <p>魔法师</p> <p>科举夺魁</p> <p>武将</p> <p>江湖奇侠',
'autoupdate' => '10',
'name' => '问吧',
'moduledir' => 'wenba',
'moduledomain' => '',
'linkurl' => '/opensource/phpcms2007_sp6_gbk/phpcms/wenba/',
);
?>

 

include/global.func.php
更新模块设置函数

 

 
代码如下:

function module_setting($module, $setting)
{
global $db,$MODULE,$LANG;
if(!is_array($setting) || !array_key_exists($module,$MODULE)) return FALSE;
if(isset($setting['moduledomain']))
{
$moduledomain = $setting['moduledomain'];
$db->query("UPDATE ".TABLE_MODULE." SET moduledomain='$moduledomain' WHERE module='$module'");
unset($setting['moduledomain']);
}
$setting = addslashes(serialize(new_stripslashes($setting)));
//将某个模块的多个设置的值经数组序列化以后保存在一个字段setting中
$db->query("UPDATE ".TABLE_MODULE." SET setting='$setting' WHERE module='$module'");
cache_module($module);
cache_common();
return TRUE;
}

本文地址:https://www.freemoban.com/jzbz/2015/0708/2277.html

猜你喜欢
栏目推荐
模板推荐

Copyright:www.freemoban.com 免费模板网 All Rights Reserved 网站备案:辽ICP备19014872号-2   辽公网安备 21010602000376号  辽公网安备:42900402000182号

免责声明:本站部分资源来自互联网收集,版权归原创者所有,如果侵犯了你的权益,我们会及时删除侵权内容,联系QQ:1615187561 谢谢合作!