phpcms缓存使用总结(memcached、eaccelerator、shm)


本文整理自网络,侵删。

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

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

与b的缓存原理类似,只是此处生成的文件名是根据计算$PHP_SELF与$PHP_QUERYSTRING的md5值生成的文件名,相对于所有php动态页面来说都是一样的,这个思想比较精典,值得借签
以问吧模块为例进行说明
文件调用顺序为:index.php -> js.php -> ad.php -> global.func.php
用http://www.chf.com/opensource/phpcms2007_sp6_gbk/phpcms/wenba/进行访问
此目录下有个index.php文件,判断当前目录下是否存在名为index_cache.html的文件,如果有,则直接包含此文件,如果不存在此文件,则动态地读取完数据后保存在index_cache.html文件,以备下次使用

用上述的url访问时,页面里面包含有如下的一行js代码
<script language="javascript" src="/opensource/phpcms2007_sp6_gbk/phpcms/data/js.php?id=1"></script>
此js代码其实就是动态调用php页面的内容
http://www.chf.com/opensource/phpcms2007_sp6_gbk/phpcms/data/js.php?id=1

js.php文件的内容:


代码如下:

<?php
chdir('../ads/');
require './ad.php';
?>

ad.php的内容:


代码如下:

<?php
define('SHOWJS', 1);
require './include/common.inc.php';
require MOD_ROOT.'/include/global.func.php';</p> <p>$placeid = intval($id);</p> <p>$query ="SELECT * FROM ".TABLE_ADS." AS a LEFT JOIN ".TABLE_ADS_PLACE." AS p ON (a.placeid=p.placeid) WHERE a.placeid=".$placeid." AND a.fromdate<=UNIX_TIMESTAMP() AND a.todate>=UNIX_TIMESTAMP() AND p.passed=1 AND a.passed=1 AND a.checked=1 ORDER BY a.addtime";
$ads = $db->get_one($query, "CAHCE", 10240);
if(!$ads) exit('document.write("")');</p> <p>$db->query("UPDATE ".TABLE_ADS." SET views=views+1 WHERE adsid=".$ads['adsid']);</p> <p>$content = ads_content($ads);
$templateid = $ads['templateid'] ? $ads['templateid'] : 'ads';
include template('ads', $templateid);
phpcache();
?>

ad.php里面调用了phpcache函数,参考文件include/global.func.php


代码如下:

function phpcache($is_js = 0)
{
global $CONFIG,$cachefiledir,$cachefile;
if(!$is_js && $CONFIG['phpcache'] != '2') return FALSE;
$contents = ob_get_clean(); //读取缓冲区里面的内容
if($is_js) $contents = strip_js($contents);
if($CONFIG['phpcache'] == '2' && $cachefiledir && $cachefile)
{
dir_create($cachefiledir);
file_put_contents($cachefile, $contents); //在这儿生成一个.html格式的文件,当下次以同样的url访问时,会直接读取缓存了,参见include/common.inc.php中的代码,这儿的代码是非常非常精典的,大家好好借鉴、好好模仿吧
@chmod($cachefile, 0777);
}
/*
向浏览器发送http header,跟浏览器说,此页面不缓存,还告诉浏览器页面的最后修改时间
第一次访问js.php?id=1时向浏览器发送http header,第二次或以后再访问此url时,由于上次已经生成了缓存,所以在include/common.inc.php中直接调用缓存文件了,直到 缓存失效后再次执行此处的动态代码。此处发送的header控制缓存是相对于浏览器来说的;而通过file_put_contents生成的缓存是相对于 电脑硬盘来说的,是不一样的。
*/
header('Expires: Mon, 26 Jul 2000 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
echo $contents;
}

阅读剩余部分

相关阅读 >>

phpcms v9去除盛大连接 关闭盛大通行证的修改方法

phpcms v9出现can not connect to mysql server错误的原因和解决方法

完美解决phpcms图片太大撑破表格图片自适应图片按比例缩小

phpcms v9调用自定义字段的方法

关于cms的选择几点建议

phpcms v9网站生成sitemap静态地图页面操作步骤

phpcms类别管理教程

php 实现base64编码文件上传出现问题详解

phpcms v9 添加二级导航的思路详解

phpcms v9自带采集模块功能体验

更多相关阅读请进入《phpcms》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...