<?PHP class cache{ # 展示class的属性 var $cacheDirectory; # 缓存目录(路径) var $cacheFileName; # [缓存文件]名 var $cacheLimitedTime; # 设置过期时间周期 var $cacheExtensionName; # 设置[缓存文件]的自定义后缀名字 var $phpFileContent; # 动态读取php文件的输出内容 var $cacheError; # 错误信息 var $startTime; var $endTime;
这个是经过调整的... 测试方法是这样的.如果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 info(){
echo "\n<!--\n";
echo "缓存类名 : ".get_class($this)."\n";另外,能不能添加一个属性,可以强行按照设定的时间更新CACHE?现在是内容一变动就重新更新缓存。一点点建议,呵呵!
我刚才实际应用了一下,对于一些显示访问量的页面,每次访问,访问量最少会加一,就是说,这样的页面,每次访问都是不一样的,所以你的类会生成很多个文件。我能问一下,你的这个类实际应用是怎么用的?能说一下吗?
就是说你是怎么调用你的缓存文件的。
我是这样的命名文件的
按照$_GET和$_POST及缓存内容的md5杂凑值构成的
所以当这三者其中之一发生变化的话,都要产生缓存文件
即使缓存文件存在,我也要执行所有的程序,来生成缓存文件名来判断是否有这个文件。
既然我都执行完了,那缓存的好处就体现不出来了啊?^_^
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();
}
} # 输出缓存
function outCache(){
$this->phpFileContent = ob_get_contents();
//ob_end_flush();
if( !file_exists($this->get_CacheFileName()) ){
if(!$this->creatCacheFile()){
$this->cacheError = "创建缓存文件错误!";
$this->get_CacheError();
}
$flag = true;
}
else{
# 关闭缓存
ob_end_clean();
# 读缓存
if(!$this->readCacheFile()){
$this->cacheError = "读取缓存文件错误!";
$this->get_CacheError();
}
}
$this->deleteExpireFile();
} # 得到缓存文件
function get_CacheFileName(){
$this->cacheFileName = $this->get_CacheDirectory()."/".md5($this->makeCacheFileNameString()."_".$this->phpFileContent).$this->cacheExtensionName;
return $this->cacheFileName;
}
# 创建缓存文件
function creatCacheFile(){
$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",900);
for($i=0;$i<100;$i++){
echo $i."<br>\n";
}
$c->outCache();
echo $c->info();?>修改后的类.
TO dreamfly_whj(梦飞):
我刚才实际应用了一下,对于一些显示访问量的页面,每次访问,访问量最少会加一,就是说,这样的页面,每次访问都是不一样的,所以你的类会生成很多个文件。我能问一下,你的这个类实际应用是怎么用的?能说一下吗?
就是说你是怎么调用你的缓存文件的。
---------------------------------------------
同意。每次访问,那些页面的访问量都改变了,也就是说,必须更新缓存,才能给下一个浏览者正确显示。这样似乎缓存就无意义了。
另外,现在网上很多缓存类,smartemplate也有,和楼主的,有何不同?
你说在用法上会感觉其他的那些不爽,能否具体说明?
缓存我还没用过。
# 输出缓存
function outCache(){
$this->phpFileContent = ob_get_contents();
//ob_end_flush();
if( !file_exists($this->get_CacheFileName()) ){
if(!$this->creatCacheFile()){
$this->cacheError = "创建缓存文件错误!";
$this->get_CacheError();
}
$flag = true;
}
else{
# 关闭缓存
ob_end_clean();
# 读缓存
if(!$this->readCacheFile()){
$this->cacheError = "读取缓存文件错误!";
$this->get_CacheError();
}
}
$this->deleteExpireFile();
}
这里在执行完原有程序后才判断缓存文件是否存在,已经不对了
应该现判断缓存文件是否存在,如存在再判断文件是否过期,过期就删除重新生成,没有过期就include
------------------------
这里在执行完原有程序后才判断缓存文件是否存在,已经不对了
应该现判断缓存文件是否存在,如存在再判断文件是否过期,过期就删除重新生成,没有过期就include
------------------------
你看好代码,看构造缓存文件名的方式....
你在两个地方的理解都错了。
$c = new cache("./c1",".html",900);
for($i=0;$i<100;$i++){
echo $i."<br>\n";
}
$c->outCache();
echo $c->info();
dreamfly_whj(梦飞)
呵呵,你理解错误,可能是我写的有问题吧
下次写了东西可不能在这里帖了,csdn的构架令我佩服,昨天把我的机器弄死5次以上,速度很慢很慢
建议csdn.net/ 重新考虑一下论坛结构。
---------------------------------------------
csdn的坛子好象不支持W3C标准
作为一个技术论坛,不应该啊
看了一下
功能比我的那个多
不过我是不想把用户限的太死
for($i=0;$i<10000;$i++){
echo $i."<br>555\n";
}
$fp = fopen($_SERVER["DOCUMENT_ROOT"]."/test/c1/log.txt","a");
fwrite($fp,date("H:i:s")."\n");
fclose($fp);$c->outCache();
echo $c->info();
在楼主的cache执行中加入这样的代码,你会发现.即使cache文件存在,只是执行了本页.但不输出结果,然后再调用已有的cache文件.这样只要增加服务器的负担的...你可以试一下.第一次执行都会在log.txt中加入行时间值的..说明即使有cache的时候.原文件还是运行了一次,只是没有输出而已...
流程应该重新调整一下..
测试方法是这样的.如果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();
?>