




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
phpcms使用方式总结a.模板编译缓存参考文件include/global.func.php及include/template.func.php模板编译缓存的原理其实很简单,如果模板是第一次编译,则直接编译它,如果不是第一次编译,则比较模板文件($tplfile)及模板缓存文件 ($compiledtplfile)的修改时间,如果模板文件的修改时间大于编译过的模板缓存文件,则编译模板,否则不编译模板,提高了程序的执行效率。function template($module = phpcms, $template = index) global $CONFIG; $compiledtplfile = $CONFIGtemplatescachedir.$module._.$template.tpl.php; if($CONFIGtemplaterefresh) $tplfile = PHPCMS_ROOT./templates/.$CONFIGdefaulttemplate./.$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的缓存原理类似,只是此处生成的文件名相对固定以问吧模块为例进行说明用/opensource/phpcms2007_sp6_gbk/phpcms/wenba/进行访问此目录下有个index.php文件,判断当前目录下是否存在名为index_cache.html的文件,如果有没有过失效期,则直接包含此文件,否则动态地读取完数据后保存为index_cache.html文件,以备下次使用。文件index.php中的内容:?phprequire_once ./include/common.inc.php;$lastedittime = filemtime(index_cache.html);$lastedittime = $PHP_TIME-$lastedittime;$autoupdatetime = intval($MODautoupdate); /$MODautoupdate来自缓存文件data/cache/wenba_setting.php中的内容if(file_exists(index_cache.html) & $lastedittime怎么判断文件是否失效呢,文件data/cache/wenba_setting.php中有如下的设置,其中字段autoupdate的值就是文件失效的时间,单位是秒,在后台可以进行设置文件wenba_setting.php是从哪儿来的呢,进行安装时自动把各种模块的数据保存到数据库中了,安装时就生成缓存数据了,在include/common.inc.php中函数cache_all也可以生成缓存,后台进行设置时cache会自动更新的 100,anybody_score = 2,answer_give_credit = 5,vote_give_credit = 1,highscore = 2,vote_give_actor = 公司白领魔法师科举夺魁武将江湖奇侠,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($settingmoduledomain) $moduledomain = $settingmoduledomain; $db-query(UPDATE .TABLE_MODULE. SET moduledomain=$moduledomain WHERE module=$module); unset($settingmoduledomain); $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用/opensource/phpcms2007_sp6_gbk/phpcms/wenba/进行访问此目录下有个index.php文件,判断当前目录下是否存在名为index_cache.html的文件,如果有,则直接包含此文件,如果不存在此文件,则动态地读取完数据后保存在index_cache.html文件,以备下次使用用上述的url访问时,页面里面包含有如下的一行js代码此js代码其实就是动态调用php页面的内容/opensource/phpcms2007_sp6_gbk/phpcms/data/js.php?id=1js.php文件的内容:ad.php的内容:?phpdefine(SHOWJS, 1);require ./include/common.inc.php;require MOD_ROOT./include/global.func.php;$placeid = intval($id);$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 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();$db-query(UPDATE .TABLE_ADS. SET views=views+1 WHERE adsid=.$adsadsid);$content = ads_content($ads);$templateid = $adstemplateid ? $adstemplateid : ads;include template(ads, $templateid);phpcache();?ad.php里面调用了phpcache函数,参考文件include/global.func.phpfunction phpcache($is_js = 0) global $CONFIG,$cachefiledir,$cachefile; if(!$is_js & $CONFIGphpcache != 2) return FALSE; $contents = ob_get_clean(); /读取缓冲区里面的内容 if($is_js) $contents = strip_js($contents); if($CONFIGphpcache = 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;上面的phpcache函数中的全局变量$cachefiledir,$cachefile是从哪里来的呢,从这儿来的文件include/common.inc.php中的内容if(!defined(IN_ADMIN) if($CONFIGdbiscache) $db_file .= _cache; if($CONFIGphpcache = 2) $cachefileid = md5($PHP_SELF.?.$PHP_QUERYSTRING); $cachefiledir = PHPCMS_ROOT./data/phpcache/.substr($cachefileid, 0, 2)./; $cachefile = $cachefiledir.$cachefileid.html; /echo cachefile:$cachefile; if(file_exists($cachefile) & ($PHP_TIME connect($CONFIGdbhost, $CONFIGdbuser, $CONFIGdbpw, $CONFIGdbname, $CONFIGpconnect);$db-iscache = $CONFIGdbiscache; /是否启用 sql cache (只对前台起作用,建议在不生成html并且访问量过大时开启)$db-expires = $CONFIGdbexpires; /sql cache 过期时间(秒)db_mysql_cache.class.php中的代码function query($sql , $type = , $expires = 3600, $dbname = ) if($this-isclient) $dbname = $dbname ? $dbname : $this-dbname; $this-select_db($dbname); /* $this-iscache表示是否启动了数据库查询缓存 如果启用了数据库查询缓存且$type为CACHE且是select语句,则启用查询缓存 个人感觉这儿$type参数用strtoupper处理一下更好了 */ if($this-iscache & $type = CACHE & stristr($sql, SELECT) $this-caching = 1; /成员变量caching标识启用了数据库查询缓存,用在下面的fetch_array,num_rows,free_result函数中,其实用iscache就可以判断了,没有必要再用一个成员变量了 $this-expires = $expires; /数据库缓存数据的失效期 return $this-_query_cache($sql); /然后调用_query_cache方法 $this-caching = 0; $func = $type = UNBUFFERED ? mysql_unbuffered_query : mysql_query; if(!($query = $func($sql , $this-connid) & $type != SILENT) $this-halt(MySQL Query Error, $sql); $this-querynum+; return $query; function _query_cache($sql) $this-cache_id = md5($sql); /计算$sql的md5值,然后作为cache_id $this-result = array(); $this-cursor = 0; $this-cache_file = $this-_get_file(); /得到cache文件名 /如果cache数据已经过期,则重新从数据库中取得查询结果,然后保存在数据库中 if($this-_is_expire() $this-result = $this-_get_array($sql); /从数据库中取结果 $this-_save_result(); /保存结果到缓存数据中 else $this-result = $this-_get_result(); /缓存没过期直接取缓存数据 return $this-result; function _get_file() global $CONFIG; /cache文件的主目录一般是data/dbcache return $CONFIGdbcachedir.substr($this-cache_id, 0, 2)./.$this-cache_id.php; function _is_expire() global $PHP_TIME; return !file_exists($this-cache_file) | ( $PHP_TIME filemtime($this-cache_file) + $this-expires ); /*由于方法_get_array只是被方法_query_cache调用,所以在此方法里面直接用函数mysql_unbuffered_query了,因为mysql_unbuffered性能好一点,参考/viewthread.php?tid=958067&extra=page%3D4*/function _get_array($sql) $this-cursor = 0; $arr = array(); $result = mysql_unbuffered_query($sql, $this-connid); while($row = mysql_fetch_assoc($result) $arr = $row; $this-free_result($result); $this-querynum+; return $arr; function _save_result() if(!is_array($this-result) return FALSE; dir_create(dirname($this-cache_file); file_put_contents($this-cache_file
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025四川泸州市叙永县卫生健康局招聘定向医学专科生2人考试参考试题及答案解析
- 2025年公安信息试题及答案
- 游戏题库节选题及答案
- 2025贵州毕节市七星关区教育局下属事业单位专项引进体育领域专业人才计划22人考试参考题库附答案解析
- 2025广东珠海市教育局直属公办学校招聘临聘教行辅人员考试参考题库附答案解析
- 2025广东招商银行东莞分行社会招聘笔试参考题库附答案解析
- 2025年智能电网改造工程设备供应与安装合同
- 2025年环保节能PE管材工程全流程服务合同
- 2025年高端医疗设备租赁与全面安全保障及维护服务协议
- 2025年度高端离婚财产分割特色旅游项目及基金共建协议
- 电子竞技赛事策划与组织运营管理方案设计
- 人教版(2024)八年级上册数学全册教案
- 2025年智慧城市信息化运维服务合作合同模板
- 职工职业健康体检实施方案与标准
- 2025年多省公务员联考公安基础知识考试真题(附答案)
- 2025年税务副科领导干部面试题及答案
- 基孔肯雅热培训测试题含答案
- 2022.12六级真题第3套答案及详解
- 七下地理知识清单
- 基于人工智能的复合材料结构性能预测及分析方法研究
- 股权无偿转让与公司资产重组协议
评论
0/150
提交评论