PHP缓存, SimpleCache 类

本文介绍了一个简单的PHP页面缓存机制——SimpleCache类,通过此类可以实现页面缓存,提高网站性能。类包括缓存的启动、结束、判断缓存是否过期、获取文件名和创建缓存等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个简单的不能再简单的页面缓存,效果还是比较明显的

<?php
/* 页面缓存机制
* 摘要:页面缓存机制
* 类名:SimpleCache
* 文件名称:SimpleCache_class.php
* 版本:0.0.2
* 创建时间:2004.7.24 03:38
* 修改时间:2004.11.28 23:28
* 作者:shadow_wwp@yahoo.com
* 版权:COPYRIGHT(C) 2004-2005 流影工作室
*/

/* 使用方法
    require_once("SimpleCache.php");
    $effect_param = array(
        "a","b","c","d","e"
    );
    
    $effect_flag_param = array(
        "a", "b", "c"
    );
    $cache = new SimpleCache();
    $cache->start(5,$effect_param,$effect_flag_param);
    //-- ---//
    echo "HHHH";
    
    //-- --//
    
    $cache->end();
*/

define("CACHE_NO_CREATE", "NO"
);
define("CACHE_CREATE","YES"
);
define("CACHE_CREATE_FAULT","ERR"
);

//缓存类路径
define("ROOT","/data1"
);
define("CACHE",ROOT."/cache"
);

class
SimpleCache
{
    var
$filename;  
//要保存的文件名称
    
var $flag
;
    var
$effect_time
;
    var
$cache_error
;
    
    
//以下是 0.0.2 版更新
    
var $effect_param = Array();
//有效参数
    
var $effect_flag_param = Array();
//标记型参数
    
var $cache_path;     
//要保存文件的目录名称

    //启用缓存
    //$flag CACHE_CREATE  ob_start 启动
    //$flag CACHE_NO_CREATE 不需要创建缓存 读取缓存
    //$flag CACHE_CREATE_FAULT 创建缓存失败
    //0.0.2 版更新
    
function start($effect, $effect_param, $effect_flag_param
)
    {
        
$this->effect_time = $effect
;
        if(!
is_array($effect_param
))
        {
            
$this->flag  = CACHE_CREATE_FAULT
;
            
$this->cache_error .= "/n"."/$effect_param 不是数组"
;
        }
        else
        {
            
$this->effect_param = $effect_param
;
        }
        
        if(!
is_array($this->effect_flag_param
))
        {
            
$this->flag  = CACHE_CREATE_FAULT
;
            
$this->cache_error .= "/n"."effect_flag_param 不是数组"
;
        }
        else
        {
            
$this->effect_flag_param = $effect_flag_param
;
        }
        
        
$this->is_expire
();
        
        if (
$this->flag == CACHE_NO_CREATE)   
//读取信号 看是否应该创建
        
{
            
$this->cache_read
();
        }

        if (
$this->flag == CACHE_CREATE
)
        {
            if (!
ob_start
())
            {
                
$this->flag  = CACHE_CREATE_FAULT
;
                
$this->cache_error .= "/n"."ob_start()失败!/n"
;
            }
            
            
        }

        
//debug echo "/n<!-- 创建信号为:".$this->flag."-->/n";
    
}


    
//启用缓存结束
    
function end
()
    {
        
        if (
$this->flag == CACHE_CREATE
)
        {
            
//创建文件
            
if(!$this->create_cache
())
            {
                
$this->flag = CACHE_CREATE_FAULT
;
                
$this->cache_error .= "/n"."创建缓存失败/n"
;
                exit(
0
);
            }

            
//标记下次不用创建
            
$this->flag = CACHE_NO_CREATE
;
        

        }
    }


    
//判断缓存文件是否过期
    //0.0.2 版本优化
    
function is_expire
()
    {
        if (!
$this->get_filename
())
        {
            
$this->cache_error .= "/n"."不能生成文件名称!"
;
            
$this->flag = CACHE_CREATE_FAULT
;
            return
0
;
        }
        
        
        
$now = time
();
        
        if (!
file_exists($this->filename
))
        {
            
$this->flag = CACHE_CREATE
;
            return
0
;
        }

        
$createtime = filemtime($this->filename
);
        
$time_field = $now - $createtime
;
        
//debug echo "/n<!-- 时间差为".$time_field."-->/n";

        
if ($time_field > $this->effect_time
)
        {
            
$this->flag = CACHE_CREATE
;
            return
0
;
        }
        
        
$this->flag = CACHE_NO_CREATE
;
    }

    
//取得文件名称
    //命名规则是: 参数_参数值_创建的文件名称
    //0.0.2 版本优化
    
function get_filename
()
    {
        if (!
$this->get_dir
())
        {
            
$this->cache_error .= "/n"."不能获得目录"
;
            return
false
;
        }
        foreach (
$this->effect_param as $key
)
        {
            
$this->filename = $this->filename."_".$key."_".$_REQUEST[$key
];
        }

        
$this->filename = md5($this->filename). "_" .basename($_SERVER["PHP_SELF"
]);
        
$this->filename = $this->cache_path ."/". $this->filename
;
        
        return
true
;
    }
    
    
    
//取得目录名称 如果目录不存在则创建    
    //0.0.2版增加
    
function get_dir
()
    {
        
$this->cache_path = CACHE
;
        
        foreach (
$this->effect_flag_param as $key
)
        {
            
$this->cache_path = $this->cache_path."/".md5($_REQUEST[$key
]);
    
            if(!
$this->check_dir
())
            {
                return
false
;
            }
        }
        
        return
true
;
    }
    
    
//检测目录名称 如果目录不存在则创建
    //0.0.2版增加
    
function check_dir
()
    {

        if(!
is_dir($this->cache_path
))
        {
            if(!
mkdir($this->cache_path
))
            {
                
$this->cache_error .= "/n"."不能创建缓存目录!"
;
                    return
false
;
            }
        }
    
        return
true
;
    }
    
    
    
    
//从缓存中取数据
    
function cache_read
()
    {
        include(
$this->filename
);
        die();
    }

    
//生成缓存页面
    
function create_cache
()
    {
        
$this->info
();
        
        
//将本页内容写入文件
        
if(!($fp = fopen($this->filename, "w"
)))
        {    
            return
false
;
        }

        if (!
$fp
)
        {
            echo
$this->filename
;
        }
    
        
        
flock($fp, 2
);
        if (!
fputs($fp, ob_get_contents
()))
        {
            return
false
;
        }
        
flock($fp, 3
);
        
fclose($fp
);
        
//写入完成
        
ob_end_flush
();
    
//    ob_end_clean();
        
return true
;
    }

    
//打印调试信息
    
function info
()
    {
        
#echo "/n<!--/n";
        
echo "创建时间:".time()."/n"
;
        echo
"文件名称:".$this->filename."/n"
;
        echo
"有效期限:". $this->effect_time."/n"
;
        echo
"缓存目录:".$this->cache_path ."/n"
;
        echo
"创建的缓存名称:".basename($_SERVER["PHP_SELF"])."/n"
;
        echo
"---------------------------------------------/n"
;
        echo
$this->cache_error."/n"
;
        echo
"---------------------------------------------/n"
;
        
#echo "-->/n";
    
}

}
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值