这个是经过调整的... 测试方法是这样的.如果cache文件不存在就会在c2的目录下面建立一个log.txt并写入cache建立的时间.以后每建立一次cache文件就会在这里写入建立的时间,如果你刷新页面,这里没有建立时间就表示当前页面执行得是cache文件的内容.过程很简单.是在楼主的代码基础上修改的. <?PHP class cache{ # 展示class的属性 var $cacheDirectory; # 缓存目录(路径) var $cacheFileName; # [缓存文件]名 var $cacheLimitedTime; # 设置过期时间周期 var $cacheExtensionName; # 设置[缓存文件]的自定义后缀名字 var $phpFileContent; # 动态读取php文件的输出内容 var $cacheError; # 错误信息 var $startTime; var $endTime;
是怎样进行缓存,高手说Discuz的每个地方,都做了缓存。
因为,论坛是实时更新的。
再几个效率高的缓存类和适应方法
谢谢了
测试方法是这样的.如果cache文件不存在就会在c2的目录下面建立一个log.txt并写入cache建立的时间.以后每建立一次cache文件就会在这里写入建立的时间,如果你刷新页面,这里没有建立时间就表示当前页面执行得是cache文件的内容.过程很简单.是在楼主的代码基础上修改的.
<?PHP
class cache{
# 展示class的属性
var $cacheDirectory; # 缓存目录(路径)
var $cacheFileName; # [缓存文件]名
var $cacheLimitedTime; # 设置过期时间周期
var $cacheExtensionName; # 设置[缓存文件]的自定义后缀名字
var $phpFileContent; # 动态读取php文件的输出内容
var $cacheError; # 错误信息
var $startTime;
var $endTime;
# 构造函数
function cache($directory_="./cache/",$ext_=".html",$time_=900){
$this->startTime = time();
$this->cacheDirectory = $directory_;
$this->cacheExtensionName = $ext_;
$this->cacheLimitedTime = $time_;
$this->get_CacheDirectory();
if(!ob_start()){
$this->cacheError = "启动ob_start()错误!";
$this->get_CacheError();
}
} # 输出缓存(这里做了修改.如果有cache文件就输出并返回true,没有就返回false;)
function outCache(){
if( file_exists($this->get_CacheFileName()) ){
# 读缓存
if(!$this->readCacheFile()){
$this->cacheError = "读取缓存文件错误!";
$this->get_CacheError();
}else{
$this->deleteExpireFile();
}
return true;
}else{
return false;
}
} # 得到缓存文件
function get_CacheFileName(){
$this->cacheFileName = $this->get_CacheDirectory()."/".md5($this->makeCacheFileNameString()."_".$this->phpFileContent).$this->cacheExtensionName;
return $this->cacheFileName;
}
# 创建缓存文件
function creatCacheFile(){
$this->phpFileContent = ob_get_contents();
ob_end_flush();
$fp = fopen($this->cacheFileName,"w");
flock($fp, 2);
if(!fputs($fp, $this->phpFileContent)){
return false;
}
flock($fp, 3);
fclose($fp);
return true;
}
# 构造缓存文件名
function makeCacheFileNameString(){
$file_ = basename($_SERVER['PHP_SELF']);
$PHP_GET_VARS_ = "";
foreach($_GET as $key1 => $val1){
$PHP_GET_VARS_ .= $key1."_".$val1."_";
}
$PHP_POST_VARS_ = "";
foreach($_POST as $key2 => $value2){
$PHP_POST_VARS_ .= $key2."_".$value2."_";
}
$pre_str_ = $file_."_".$PHP_GET_VARS_.$PHP_POST_VARS_;
//$tmpFileName = md5($pre_str_)."_".md5($this->phpFileContent);
return $pre_str_;
}
# 读取缓存文件
function readCacheFile(){
if(!include_once($this->cacheFileName)){
return false;
}
return true;
} # 得到缓存路径(目录)
function get_CacheDirectory(){
$dir = realpath($this->cacheDirectory);
if(!is_dir($dir)){
$directoryArray = explode("/",$dir);
for($i=0;$i<count($directoryArray);$i++){
$childDirectory .= $directoryArray[$i].'/';
if (!is_dir($childDirectory)){
mkdir($childDirectory, 0777);
}
}
}
return $this->cacheDirectory;
} # 删除过期文件
function deleteExpireFile(){
# $this->cacheLimitedTime
$current_time_ = time();
$handle = opendir($this->get_CacheDirectory());
while(false != ($file = readdir($handle)) ) {
if($file != "." && $file != "..") {
if($current_time_ - filemtime($this->get_CacheDirectory()."/".$file) > $this->cacheLimitedTime){
unlink($this->get_CacheDirectory()."/".$file);
}
}
}
closedir($handle);
}
# debug 抛出错误信息
function get_CacheError(){
printf("Error : <font color=\"#FF0000\">%s</font>",$this->cacheError);
exit;
} # 输出class信息
function info(){
$this->endTime = time();
echo "\n<!--\n";
echo "缓存类名 : ".get_class($this)."\n";
echo "创建时间 : ".time()."\n";
echo "缓存路径 : ".$this->cacheDirectory."\n";
echo "文件名称 : ".$this->cacheFileName."\n";
echo "有效期限 : ". $this->cacheLimitedTime." seconds.\n";
echo "创建的PHP : ".basename($_SERVER["PHP_SELF"])."\n";
echo "执行时间 : ".$this->endTime - $this->startTime." seconds.\n";
echo "-->\n";
}
}# end class
//这里是测试代码
$c = new cache("./c1",".html",60);
//这里做了修改,如果有cache文件就直接输出.其它的都不输出了.并在outCache中删除过期的cache文件
if($c->outCache()){
echo "这里是输入cache的";
exit;
}//下面是本地的执行代码.
//===========================================================
for($i=0;$i<1000;$i++){
echo $i."<br>555\n";
}
$fp = fopen($_SERVER["DOCUMENT_ROOT"]."/test/c2/log.txt","a");
fwrite($fp,date("H:i:s")."\n");
fclose($fp);
//===========================================================//如果程序能执行到这里.说明没有cache文件,这里建立就行了.
$c->creatCacheFile();
?>
就是检测时间有没有过期,文件有没有存在,如果过期或者不存在,那么就创建它
在一定的时间段内,新的请求来到时直接使用缓存文件
过期了,就创建新的缓存文件把你要缓存的东西塞到一个class里面
直接序列化/反序列化比较简便
谁能否说一下 Discuz、PHPwind。
是怎样进行缓存,高手说Discuz的每个地方,都做了缓存。
因为,论坛是实时更新的。
再几个效率高的缓存类和适应方法
谢谢了
同意,强烈关注这个。
然后用户显示列表时候如果没更新,就打开换村?还是时间控制,好象csdn,有时后csdn结贴了帖子还在显示,说明threads表有对应的xml文件,有人好象说过3分钟,个人觉得好象会判断服务器资源然后做出判断,其实我觉得用上xml,然后有zend o+e,再有smarty的凑货换村,应该还可以,主要市结构控制好,哎刚刚睡醒,胡言乱语