dedecms织梦内容管理系统    
首页 | java | C/C++ | PHP | 操作系统 | ajax | 脚本编程 | 安全技术 | 本站下载页 | flex | CRM | 专题 | QQ群 | 测试中心 | 会员中心 | 积分规则
  当前位置:主页>PHP>php技术资料>文章内容
php的缓存类
来源: 作者:
<?php
 
class cache {
    
var $_file;
    
var $cache_time;
 
    
function cache($_file='_index.htm',$cache_time=1) {
        
$this->_file        = $_file;
        
$this->cache_time    = $cache_time;
    
}
 
    
/*
    * Start cache method without Return
    */

    
function start() {
      
        
if($this->cache_is_active()) {
            
include($this->_file);
            
exit;
            
}
          
ob_start();
    
}
 
    
/*
    * End of cache method without Return
    */

    
function _end() {
        
$this->make_cache();
        
ob_end_flush();
    
}
 
    
/*
    * Check if cache file is actived
    * Return true/false
    */

    
function cache_is_active() {
        
if ($this->cache_is_exist()) {
            
if (time() - $this->lastModified() < $this->cache_time)
                
Return true;
            
else {
                
Return false;
            
}
        
}
        
else {
            
Return false;
        
}
    
}
 
    
/*
    * Create cache file
    * Return true/false
    */

    
function make_cache() {
        
$content    = $this->get_cache_content();
        
if($this->write_file($content)) {
            
Return true;
        
}
        
else {
            
Return false;
        
}
    
}
 
    
/*
    * Check if cache file is exists
    * Return true/false
    */

    
function cache_is_exist() {
        
if(file_exists($this->_file)) {
            
Return true;
        
}
        
else {
            
Return false;
        
}
    
}
 
    
/*
    * Return last Modified time in bollin formart
    * Usage: $lastmodified = $this->lastModified();
    */

    
function lastModified() {
        
Return @filemtime($this->_file);
    
}
 
    
/*
    * Return Content of Page
    * Usage: $content = $this->get_cache_content();
    */

    
function get_cache_content() {
        
$contents = ob_get_contents();
//        Return '<!--'.date('Y-m-d H:i:s').'-->'.$contents;
        
Return $contents;
    
}
 
    
/*Write content to $this->_file
    * Return true/false
    * Usage: $this->write_file($content);
    **/

    
function write_file($content,$mode='w+')
    
{
        
$this->mk_dir($this->_file);
        
if (!$fp = @fopen($this->_file,$mode)) {
            
$this->report_Error($this->_file." 目录或者文件属性无法写入.");
            
Return false;
        
} else{
            @
fwrite($fp,$content);
            @
fclose($fp);
            @
umask($oldmask);
            
Return true;
        
}
    
}
 
    
/*
    * Make given dir included in $this->_file
    * Without Return
    * Usage: $this->mk_dir();
    */

    
function mk_dir()
    
{    //$this->_file    = str_replace('','/');
        
$dir    = @explode("/", $this->_file);
        
$num    = @count($dir)-1;
        
$tmp    = './';
        
for($i=0; $i<$num; $i++){
            
$tmp    .= $dir[$i];
            
if(!file_exists($tmp)){
                @
mkdir($tmp);
                @
chmod($tmp, 0777);
            
}
            
$tmp    .= '/';
        
}
    
}
 
    
/*
    * Unlink an exists cache
    * Return true/false
    * Usage: $this->clear_cache();
    */

    
function clear_cache() {
        
if (!@unlink($this->_file)) {
            
$this->report_Error('Unable to remove cache');
            
Return false;
        
}
        
else {
            
Return true;
        
}
    
}
 
    
/*
    * Report Error Messages
    * Usage: $this->report_Error($message);
    */

    
function report_Error($message=NULL) {
        
if($message!=NULL) {
            
trigger_error($message);   
        
}
    
}
}
?>

上一篇:观察者(Observer)模式优缺点   下一篇:生成静态页面的PHP类
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论
  热点文章
·通过对PHP服务器端特性的配置加
·php与mysql三日通
·AJAX技术在PHP开发中的简单应用
·大家所使用的PHP开发环境
·PHP缓存的实现
·针对PHP新手总结的PHP基础知识
·一个简单实现多条件查询的例子
·PHP串行化变量和序列化对象
·PHP表单
·推荐阅读:php技术生成静态页面
·php实用函数
·php生成随机数
  相关文章
·生成静态页面的PHP类
·用php5的simplexml解析各种feed
·什么时候用GET?什么时候用POST
·缺陷月项目披露PHP脚本语言漏洞
·PHP 命令行?是的,您可以!
·Apache服务器配置全攻略
·挑战最棒的留言本的源码(二)
·挑战最棒的留言本的源码(一)
·用php与mysql的电子贺卡程序
·用PHP调用数据库的存贮过程
·浅谈Windows下 PHP4.0与oracle 8
·用PHP连mysql和oracle数据库性能
  相关信息
copy right @ 百家拳软件项目研究室 2007 辽ICP备07011763