phpcms缓存使用总结

phpcms缓存使用总结,第1张

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

本文主要介绍phpcms中memcached、eaccelerator、shm缓存的使用方法,有需要的可以参考一下。

A.模板编译缓存
引用文件有/global.func.php和include/Template.func.PHP
模板编译缓存的原理其实很简单。如果是第一次编译模板,直接编译。如果不是第一次编译,那么比较一下模板文件($tplfile)和模板缓存文件($compiledtplfile)的修改时间。如果模板文件的修改时间比编译后的模板缓存文件长,则编译模板,否则不编译模板,提高了程序的执行效率。


复制代码如下:
函数模板($module='phpcms',$Template='index')
{
Global$config;
$compiledtplfile=$CONFIG['templatescachedir']。$模块。'_'.$template。.TPL.PHP';
if($CONFIG['templaterefresh'])
{
$TPLfile=PHPCMS_ROOT。/templates/'。$CONFIG['defaulttemplate']。'/'.$模块。'/'.$template。。html';
如果(!file_exists($compiledtplfile)||@filemtime($tplfile)>;@filemtime($compiledtplfile))
{
require_oncePHPCMS_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“包含缓存文件”;
包含“index_cache.html”;
}
else
{
echo“读取动态页面”;
...
?>

如何判断一个文件是否无效?文件data/cache/wenba_setting.php有如下设置,其中字段autoupdate的值为文件失效的时间,单位为秒。可以在后台设置
文件wenba_setting.php从哪里来?当您安装它时,您会自动将各种模块的数据保存到数据库中。缓存数据是在安装过程中生成的。函数cache_all也可以在include/common.inc.php中生成缓存,后台设置时会自动更新缓存。


复制代码如下:
<?php
返回数组(
'higth_score'=>;100',
'任何人得分'=>2',
'answer_give_credit'=>;5',
'vote_give_credit'=>;1',
'highscore'=>;2',
'vote_give_actor'=>;公司白领

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


复制代码如下:
FunctionModule_Setting($module,$Setting)
{
Global$db,$Module,$lang
如果(!is_array($setting)||!array_key_exists($module,$module))返回FALSE
if(isset($setting['moduledomain'])
{
$moduledomain=$setting['moduledomain'];
$dB->;查询(“更新”)。表_模块。"SETmoduledomain='$moduledomain',其中module='$module'";
unset($setting['moduledomain']);
}
$setting=addslashes(serialize(new_stripslashes($setting)));
//将一个模块的多个设置的值序列化到一个数组中,保存到一个字段设置中
$db-->;查询(“更新”)。表_模块。"SETsetting='$setting'其中module='$module'";
cache_module($module);
cache_common();
返回TRUE
}

C.在动态页面中生成静态缓存文件

类似于B的缓存原理,只是这里生成的文件名是通过计算$PHP_SELF和$PHP_QUERYSTRING的md5值生成的文件名,与所有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>;
这段js代码其实就是动态调用php页面的内容
http://www.chf.com/opensource/phpcms2007_sp6_gbk/PHPCMS/data/js.PHP?id=1

Js.php的内容:


复制代码如下:
<?php
chdir('../ads/');
需要'。/ad.PHP';
?>

ad.php的内容:


复制代码如下:
<?php
define('SHOWJS',1);
需要'。/include/common.Inc.PHP';
需要MOD_ROOT。/include/global.func.PHP';</p>; <;p>$placeid=intval($id);</p>; <;p>$query="SELECT*FROM"。表_广告。“作为左连接”。表_广告_地点。ASpON(a.placeid=p.placeid)其中a.placeid=。$placeid。和a.fromdate<=UNIX_TIMESTAMP()和a.todate>=UNIX_TIMESTAMP()ANDp.passed=1ANDa.passed=1ANDa.checked=1ORDERBYa.addtime";
$ads=$dB->;get_one($查询,“CAHCE”,10240);
如果(!$ads)退出('document.write(""');</p>; <;p>$dB->;查询(“更新”)。表_广告。"SETviews=views+1其中adsid=。$ads['adsid']);</p>; <;p>$content=ads_content($ads);
$templateid=$ads['templateid']?$ads['templateid']:'ads';
包含模板('ads',$templateid);
PHPcache();
?>

Ad.php调用phpcache函数,参考文件include/global.func.php


复制代码如下:
函数PHPCache($is_js=0)
{
Global$config,$cachefiledir,$cachefile
如果(!$is_js&&$CONFIG['phpcache']!='2')返回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头,告诉浏览器这个页面没有被缓存,还要告诉浏览器这个页面最后一次修改的时间。
第一次来js.php?id=1时向浏览器发送http头。当你第二次或以后访问这个url时,因为上次已经生成了缓存,所以直接调用include/common.inc.php中的缓存文件,直到缓存失败后再次执行这里的动态代码。此处发送的标题控件缓存是相对于浏览器的;file_put_contents生成的缓存和电脑硬盘不一样。
*/
header('Expires:Mon2000年7月26日05:00:00GMT');
标头('Last-Modified:'。gmdate('D,dMYH:i:s')。GMT’);
header('Cache-Control:no-Cache,must-revalidate');
头('Pragma:no-cache');
echo$contents;
}

上面phpcache函数中的全局变量$$cachefiledir,$cachefile从何而来?它来自这里
文件中的内容包括/common.inc.php


复制代码如下:
if(!defined('IN_ADMIN')
{
if($CONFIG['dbiscache'])$db_file。='_cache
if($CONFIG['PHPcache']='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<@filemtime($cachefile)+$CONFIG['phpcacheexpires'])
{
需要$cachefile;
退出;
}
}
if($PHP_querystring&;&preg_match("/^(.*)\.(htm|html|shtm|shtml)$/",$PHP_QUERYSTRING,$URLvar))
{
parse_str(str_replace(array('/','-',''),array('&;','=',''),$URLvar[1]);
}
}

D.数据库查询结果缓存
下面是include/common.inc.php中的几行代码


复制代码如下:
$db_file=$db_class='db_'。$config['database'];//$CONFIG['database']位于config.inc.php,配置可以使用自己的数据库,比如MySQL、SQLite和SQLServer
需要phpcms_root。/include/'。$db_file。。class.PHP';
$db=new$db_class;
$dB->;connect($CONFIG['dbhost'],$CONFIG['dbuser'],$CONFIG['dbpw'],$CONFIG['dbname'],$CONFIG['pconnect']);
$dB->;iscache=$CONFIG['dbiscache'];//是否启用sql缓存(只对前台有效,建议在没有生成html,流量太大的时候打开)
$db-->;expires=$CONFIG['dbexpires'];//sql缓存过期时间(秒)

db_mysql_cache.class.php中的代码


复制代码如下:
函数查询($SQL,$type='',$expires=3600,$dbname='')
{
if($this->;isclient)
{
$dbname=$dbname?$dbname:$this->;dbname
$this->;select_db($dbname);
}
/*
$this->;IsCACHE表示是否启用数据库查询缓存
如果启用了数据库查询缓存且$type为CACHE且为select语句,则查询缓存启用
个人认为,最好用strtoupper
*/
If($this-->;iscache&&$type=='CACHE'&&stristr($sql,'SELECT')
{
$this->;缓存=1;//成员变量缓存标识数据库查询缓存已启用,并在以下fetch_array、num_rows和free_result函数中使用。其实用iscache就可以判断了,不需要再用另一个成员变量
$this->;expires=$expires//数据库缓存数据的有效期
return$this->;_query_cache($SQL);//然后调用_query_cache方法
}
$this->;缓存=0;
$func=$type=='无缓冲'?“MySQL_unbuffed_query”:“MySQL_query”;
如果(!($query=$func($sql,$this->;connid))&;&$type!='SILENT')
{
$this->;halt('MySQL查询错误',$SQL);
}
$this->;querynum++;
return$query;
}<;/p>; <;p>function_query_cache($SQL)
{
$this->;cache_id=MD5($SQL);//计算$sql的md5值,作为cache_id
$this->:result=array();
$this->;光标=0;
$this->;cache_file=$this->;_get_file();//获取缓存文件名
//如果缓存数据已经过期,则再次从数据库中获取查询结果并保存在数据库中
If($this->;_is_expire())
{
$this->;result=$this->;_get_array($SQL);//从数据库中获取结果
$this->;_save_result();//将结果保存到缓存数据
}
else
{
$this-->;result=$this->;_get_result();//在缓存过期之前直接获取缓存的数据
}
return$this->;结果;
}<;/p>; <;p>function_get_file()
{
global$CONFIG;
//缓存文件的主目录一般是data/dbcache
return$config['dbcachedir']。substr($this->;cache_id,0,2)。'/'.$this->;cache_id。。PHP';
}<;/p>; <;p>function_is_expire()
{
global$PHP_TIME;
退货!file_exists($this->;cache_file)||($PHP_TIME>;@filemtime($this->;cache_file)+$this->;过期);
}<;/p>; <;p>/*
由于method_get_array只被method_query_cache调用,所以在这个方法中直接使用了函数mysql_unbuffered_query,因为mysql_unbuffered的性能更好。参考
http://bbs.chinaunix.net/viewthread.php?tid=958067。extra=page%3D4</a>;
*/
function_get_array($SQL)
{
$this->;光标=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;
}<;/p>; <;p>function_save_result()
{
if(!is_array($this->;结果))返回FALSE
dir_create(dirname($this->;cache_file));
file_put_contents($this->;cache_file,"<?php\nreturn"。var_export($this->;结果,真)。";\n?>);
@chmod($this->;cache_file,0777);
}<;/p>; <;p>function_get_result()
{
returninclude$this->;cache_file
}<;/p>; <;p>函数fetch_array($query,$result_type=MYSQL_ASSOC)
{
return$this->;缓存?$this->;_fetch_array($query):MySQL_fetch_array($query,$result_type);
}<;/p>; <;p>//从数据库中获取查询结果
function_fetch_array($result=array())
{
if($result)$this-->;result=$result
returnisset($this->;结果[$this->;光标])?$this->;结果[$this->;cursor++]:FALSE;
}<;/p>; <;p>函数num_rows($query)
{
return$this->;缓存?$this->;_num_rows($query):MySQL_num_rows($query);
}<;/p>; <;p>functionfree_result($query)
{
if($this->;caching==1)$this->;结果=array();
else@MySQL_free_result($query);
}

如果将上述文件存储改为memcached、eaccelerator、shm等。,效率会更高,改变也不太难。可以在后台添加一个设置选项,比如
files、memcached、eaccelerator、shm等。供管理员设置,然后调用相应的
存储系统进行存储。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/773019.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-03
下一篇 2022-05-03

发表评论

登录后才能评论

评论列表(0条)

保存