由于本站采用了PEAR Cache_lite作为永久缓存,所以不得不写一个插件,利用该插件实现当用户发布或者修改删除文章的时候自动清除缓存.那么这个时候就得考虑到Pear的Cache clear的效率了,如果清除缓存需要很久的话,那么发表、修改或者删除文章的时候,此插件会让操作的速度变得难以忍受。
说明:为了简单,当前我的插件实现的功能是在发布、修改或者删除文章的时候清除本站所有的缓存,这样实现肯定有局限,局限主要是清除缓存效率的问题。如果缓存太多,可能会造成速度很慢。如果谁有比较好的缓存生成和缓存删除的平衡点的话,希望您能给我留言,谢谢。
首先利用PEAR创建缓存文件,
< ?php
define('THEME_CACHE_DIR',"cache/"); //this is the cache-lite path
define('PEAR_DIR','PEAR'); //this is the pear path
/*
you don't need to edit bellows
*/
ini_set('include_path', PEAR_DIR . PATH_SEPARATOR . ini_get('include_path'));
require_once "Cache/Lite.php";
$options = array(
'cacheDir' => THEME_CACHE_DIR,
'lifeTime' => $lifeTime,
'hashedDirectoryLevel'=>2,
'pearErrorMode' => CACHE_LITE_ERROR_DIE
);
//
//设置cache id
//
$cache_it = 0;
$id_of_the_page = md5($_SERVER['REQUEST_URI']) ;
if( $CustomLifeTime[$_SERVER['REQUEST_URI']] != ""){
$options['lifeTime'] = $CustomLifeTime[$_SERVER['REQUEST_URI']] ;
}
$cache = new Cache_Lite($options);
if ($data = $cache->get( $id_of_the_page ) ) {
$cache_it = 1;
echo $data;
die();
}
else {
if( array_search( $_SERVER['REQUEST_URI'], $no_cache ) ){
$options['lifeTime'] = 1;
}
else{
include "Cache/Lite/Output.php";
$cache2 = new Cache_Lite_Output($options);
$cache2->start($id_of_the_page);
}
}
?>
这里输出HTML文件
< ?php
if( ( $cache_it == 0 ) && $cache2 ){
echo "";
$cache2->end();
}
?>
然后反复生成缓存
< ?php
while($i < 4000){
$i ++ ;
$url = "http://127.0.0.1/test_cache/cache_gen.php?id=".microtime()."&amd=" .microtime();
$fp = fopen($url,"r");
fclose($fp);
}
?>
经过多次刷新,生成了大约250M的文本文件缓存
每个缓存文件大小为
那么这样算下来,大约6000多个文件。
cache_clear.php:
< ?php
define('THEME_CACHE_DIR',"cache/"); //this is the cache-lite path
define('PEAR_DIR','PEAR'); //this is the pear path
/*
得到一个页面的执行时间,
*/
function get_microtime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
/*
you don't need to edit bellows
*/
ini_set('include_path', PEAR_DIR . PATH_SEPARATOR . ini_get('include_path'));
$time_start = get_microtime();
require_once "Cache/Lite.php";
$options = array(
'cacheDir' => THEME_CACHE_DIR,
'hashedDirectoryLevel'=>2,
'pearErrorMode' => CACHE_LITE_ERROR_DIE
);
$cache = new Cache_Lite($options);
$cache->clean();
$time_end = get_microtime();
$time = $time_end - $time_start;
echo $time;
?>
运行cache_clear.php后清除所有的cache大约用了8秒,看来效率并不高。
当然,这个结果与服务器的磁盘读些性能相关。不过我这里仅仅才6000个文件,本来用缓存的网站就应该是流量比较大的,所以肯定不止6000个文件。
所以通过上面得测试。我的缓存机制还得需要修改,需要找到一个很好的平衡点。
上次Yskin问过我和wp-cache相比怎么样,我想说的是,我的基于PEAR。用PEAR的话说就是:
Cache_Lite provides a fast, light and safe cache system. It’s optimized for file containers and protected against cache corruptions (because it uses file locking and/or hash tests).
Cache_Lite是一个快速、轻量级的缓存。由于文件锁定机制或者hash验证机制的存在,使得改机制具有防崩溃的功能…
wp-cache以plugin的方式,我个人认为既然是缓存,就别在插件那个层面去执行,所以WP-cache的速度不及本方案,至少本方案少了调用plugin这一个层面。
本方案的缺点就是安装不太方便,但是不太麻烦。WP-cache好像要修改很多wordpress的系统文件,似乎不太好。
等我想好了如何处理缓存和清除缓存之间的平衡点后,我会在本站和大家分享该实现方法的!
当前初步的想法是。要修改的那个文章的页面和首页在post、delete或者edit的时候清除缓存,其他页面定期清除缓存。高手们要更好的想法么?
其实把这个东西做成一个插件也不太难。

你现在用上了吗?
现在你清CACHE用的什么方法?很期待!
现在cache机制有变化,首页用html文件、单篇文章用PEARcache,失效时间无限长,当创建删除修改文章的时候,利用插件(很简单的几行代码)重构首页、删除单个文章的cache。至于cat分类的cache是每两小时自动清除一次