可惜现在工作了,没有大学那么多空余的时间来写写php 或者js之类的(本人是学微波通信的哈),但是最近用上了WP,而且不喜欢千篇一律的风格,所以就自己写了一个简单的风格,使本站看这基本不像传统的blog风格了,不过由于此风格导致首页调用的数据量太大,导致首页会很慢,很容易把访客吓跑,因此不得不考虑文件Cache本站数据了,其实,我的cache机制也仅仅是基于模板,不过从wp自带的页面载入时间函数的结果看,至少比没有cache节约了1/4的时间。
smarty和PEAR也是我大学的时候经常用到的php库,突然想到smarty和PEAR的缓存功能是不是可以用到theme里面。由于wp的模板不是基于smarty,况且以前本人用smarty的时候也发现了它的缓存有点小小的问题,所以本次采用了PEAR的Cache_Lite.
进入正题,首先您得有PEAR库支持,您可以到pear.php.net 去下载。你也可以先下载我这里的。
我们看看好多theme都是那几大块组成的(http://codex.wordpress.org/Template_Hierarchy),header.php footer.php,那好,我们就把cache的头文件和footer文件分别包含进去,这样就可以每个页面进行缓存了。
这里说说我用的目录结构 其中我把PEAR放到lib文件夹下,方便今后再增加lib。cache的php文件放在我的目半cosbeta下面。即ThemeCache,如图:
。
下面是cache_start.php的内容:
< ?php
/*
author cosbeta http://www.storyday.com
@version 1.0
pls read the readme file to get more details
*/
define('THEME_CACHE_DIR',$_SERVER["DOCUMENT_ROOT"]."/wordpress/wp-content/themes/cosbeta/ThemeCache"); //this is the cache path 这里是定义cache的dir,即cache的文件放在哪里
define('PEAR_DIR',$_SERVER["DOCUMENT_ROOT"].'/wordpress/lib/PEAR'); //this is the pear path这里当然是定义PEAR的路径了
$no_cache = array("/archives/36");//do not cache my guestbook不需要cache的文件的URI,这里是我的留言本
//set the page with out cache fill the page URI into the array
$CustomLifeTime = array("0"=> "0");//set the custom life time of a spcialfied URI you may do like this $CustomLifeTime = array("/index/"=> 20000,"/?p=23"=>140.....,"URI"=>lifetime);the URI is on the bottom of page source you can view from your browser这里是自定义的cache实效时间
$lifeTime = 36000 ;//default cache life time 这个是cache失效时间
/*
you don't need to edit bellows
*/
ini_set('include_path', PEAR_DIR . PATH_SEPARATOR . ini_get('include_path'));//设置php的include默认路径
require_once "Cache/Lite.php";
$options = array(
'cacheDir' => THEME_CACHE_DIR.'/cache/',
'lifeTime' => $lifeTime,
'hashedDirectoryLevel'=>2,//放置文件太多,所以设置为2如果不设置则所有cache文件都在同一个目录下,文件多了影响速度。具体可以参考pear manual
'pearErrorMode' => CACHE_LITE_ERROR_DIE
);
//设置cache的参数
//
//设置cache id
//
//if this is search file do not cache 对于searh不使用cache
if( $s != "" ){
$isCache = false;
}
else{
$isCache = false;
}
$cache_it = 0;
$id_of_the_page = md5($_SERVER['REQUEST_URI']) ;//得到cache的id uri保证唯一,这里我偷懒了,如果人间随便建立URI。可以把我的服务器填满的,所以这里有bug存在。正在改进(准备加正则表达式判断URI是不是标准的URI)
if( $CustomLifeTime[$_SERVER['REQUEST_URI']] != ""){//查询自定义cache的life time
$options['lifeTime'] = $CustomLifeTime[$_SERVER['REQUEST_URI']] ;
}
$cache = new Cache_Lite($options);
if ($data = $cache->get( $id_of_the_page ) ) {
$cache_it = 1;
echo $data;//如果cache存在,直接输入
die();//由于这里的原因,因此本cache机制不能使用部分cache
}
else {
if( array_search( $_SERVER['REQUEST_URI'], $no_cache ) ){
}
else{
include "Cache/Lite/Output.php";
$cache2 = new Cache_Lite_Output($options);
$cache2->start($id_of_the_page);//由于这里的原因,因此本cache机制不能使用部分cache,因为这里控制了输出流,如果输出未完成则不会cache的,这个也很好处理了用户中途关闭浏览器而导致cache不完整的情况。
}
}
?>
上面如果有不清楚的,参照pear mannual
下面是cache的cache_end.php文件
< ?php
if( ( $cache_it == 0 ) && $cache2 ){
echo "";
echo "\n";
$cache2->end();
}
?>
clearcache.php
clearcache很简单,点这里下载我全部源代码themecache.zip。
自己点评:虽然说这个cache也仅仅是基于模板经过我自己不太准确的测试,至少能将页面载入时间减小到原来的1/4
另外,请将ThemeCache/cache的属性设置成 0777
该日志未加标签
我没有电脑….
口水.