关于我们┊AboutMe

昵称:Hopol(■童■)

联系:QQ:18883

邮箱:info(at)18883.com

主页:www.18883.com

手机浏览 日志归档 RSS 2.0 订阅
Register | Login

php的缓存类

<?php
/*
*     FileName                     :     cache.inc.php
*     Link                             :     [url]http://blog.csdn.net/dxflingxing/[/url]
*     Author                             :     dxflingxing
*     Date                             :     2006-5-9
*     Last Modified                 :     2006-5-16
*     Version                             :     1.0.1
*     Description                     :     Cache a page in file formart
*     Notice                             :     Make sure you cache file dir can be readed and wrote
*
*     Thanks to                     :     小邪,barryonline(寒)
************************************************************
*
*     Usage                             :
*             # Cache active time half an hour
*             # This Can Auotmatic make some none exist dirs
*             # Or you can use an cache file in curent dir
*             # The Usage Such as 
*             # $cache     = new cache(string cache_name,int seconds);
*
*             require ('cache.inc.php');
*             $cache     = new cache('dir1/dir2/cache_name.htm',60*30);     
*
*             $cache->start();
*
*             # Your Page Contents With print
*             phpinfo();
*
*             $cache->_end();
*
*/
    
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($tmp0777);
                 }
                 
$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);     
             }
     }
}
?>

Tags: php, 缓存,

« 上一篇 | 下一篇 »

只显示10条记录相关文章

dede5.1 UTF8商业版 (浏览: 231, 评论: 1)
dede5.1 BGK商业版 (浏览: 255, 评论: 0)
MyPic图片管理系统 (浏览: 161, 评论: 0)
PHP 美女写真 Cms (浏览: 207, 评论: 0)
Google translator API ( PHP 5 class ) (浏览: 192, 评论: 0)
MyPic v1.0 (MyPic图片管理系统) (浏览: 288, 评论: 0)
PHP在图片上写上中文[备份] (浏览: 203, 评论: 0)
DEDECMS 模板收藏 【极限资讯网】 (浏览: 494, 评论: 0)
DEDECMS 模板收藏 【CK女性站】 (浏览: 493, 评论: 0)
DEDECMS 模板收藏 【淡蓝色完整风格V1.1】 (浏览: 499, 评论: 0)

发表评论