喜悦国际村 
» 游客:  注册 | 登录 | 搜索 | 统计 | 帮助

RSS 订阅当前论坛  

喜悦证交所已经关闭

上一主题 下一主题
 16  1/2  1  2  > 
     
标题: 发个简单的文本缓存类Cache class凑热闹  
本帖已经被作者加入个人空间
 
dxf218
注册会员
Rank: 2
一般会员



UID 24460
精华 0
积分 155
帖子 201
金钱 155 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2003-11-8
来自 北京
状态 离线
[广告]: q m
发个简单的文本缓存类Cache class凑热闹

发个简单的文本缓存类Cache class凑热闹,欢迎拍砖

<?php
/*
*    FileName                :    cache.inc.php
*    Link                    :   [url]http://blog.csdn.net/dxflingxing/[/url]
*    Author                    :   dxflingxing
*    Date                    :    2006-5-9
*    Last Modified            :    2006-5-15
*    Version                    :    1.0.0
*    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() {
        
ob_start();
        if(
$this->cache_is_active()) {
            include(
$this->_file);
            exit;
            }
    }

    
/*
    * 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').'-->'."rn".$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);    
        }
    }
}
?>
[ 本帖最后由 dxf218 于 2006-5-15 11:25 AM 编辑 ]




‘dxf零星的’BLOG:http://blog.csdn.net/dxflingxing/
2006-5-15 11:13 AM#1
查看资料  访问主页  Blog  发短消息  QQ  顶部
 
dxf218
注册会员
Rank: 2
一般会员



UID 24460
精华 0
积分 155
帖子 201
金钱 155 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2003-11-8
来自 北京
状态 离线
[推荐阅读] 自己写的第二个类,自己都感觉垃圾哦,谁来扩展一下
修正bug一处:)

<?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').'-->'."rn".$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);    
        }
    }
}
?>




‘dxf零星的’BLOG:http://blog.csdn.net/dxflingxing/
2006-5-16 11:05 AM#2
查看资料  访问主页  Blog  发短消息  QQ  顶部
 
21andy
高级会员
Rank: 4
初级会员



UID 68696
精华 0
积分 538
帖子 373
金钱 538 喜悦币
威望 0
人脉 0
阅读权限 50
注册 2006-2-18
状态 离线
[推荐阅读] 自己写的正则笔记教程
好东西!!!要顶
2006-5-24 10:10 PM#3
查看资料  发短消息  顶部
 
21andy
高级会员
Rank: 4
初级会员



UID 68696
精华 0
积分 538
帖子 373
金钱 538 喜悦币
威望 0
人脉 0
阅读权限 50
注册 2006-2-18
状态 离线
[推荐阅读] 4.0数据库查询中文时查询不到数据,怎么回事呢?
试验了,真好用啊!!!!!!!
期待下一个更完美的版本
2006-5-25 03:05 PM#4
查看资料  发短消息  顶部
 
21andy
高级会员
Rank: 4
初级会员



UID 68696
精华 0
积分 538
帖子 373
金钱 538 喜悦币
威望 0
人脉 0
阅读权限 50
注册 2006-2-18
状态 离线
[推荐阅读] 怎样设置让回帖后转到最后一楼
建议版主加精
2006-5-25 03:17 PM#5
查看资料  发短消息  顶部
 
21andy
高级会员
Rank: 4
初级会员



UID 68696
精华 0
积分 538
帖子 373
金钱 538 喜悦币
威望 0
人脉 0
阅读权限 50
注册 2006-2-18
状态 离线
[推荐阅读] 看到一个无哩头娱乐网,不知道跟咱村的无哩头有啥关系?
报告BUG一个
在PHP探针得不到自身文件路径的情况下,用这个CLASS无法创建目录和文件
2006-5-25 08:03 PM#6
查看资料  发短消息  顶部
 
mzsh (mzsh)
注册会员
Rank: 2
初级会员


UID 68858
精华 0
积分 130
帖子 111
金钱 130 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2006-2-25
状态 离线
[推荐阅读] 问两个高难度问题,能解决的人民币奖励!
function get_cache_content() {
        $contents = ob_get_contents();
        Return '<!--'.date('Y-m-d H:i:s').'-->'."rn".$contents;
    }

为什么要多写一个"rn"进去?
2006-5-25 08:25 PM#7
查看资料  发短消息  顶部
 
cogcong
高级会员
Rank: 4
南瓜茶


UID 21331
精华 0
积分 504
帖子 594
金钱 504 喜悦币
威望 0
人脉 0
阅读权限 50
注册 2003-7-3
来自 广东
状态 离线
[推荐阅读] 怎样传#符号?
勉强可以给新人学习学习....



中国非著名PHP程序员!
2006-5-25 09:19 PM#8
查看资料  发短消息  顶部
 
dxf218
注册会员
Rank: 2
一般会员



UID 24460
精华 0
积分 155
帖子 201
金钱 155 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2003-11-8
来自 北京
状态 离线
[推荐阅读] xml的显示问题!!


QUOTE:
原帖由 21andy 于 2006-5-25 08:03 PM 发表
报告BUG一个
在PHP探针得不到自身文件路径的情况下,用这个CLASS无法创建目录和文件
谢谢,确实存在BUG




‘dxf零星的’BLOG:http://blog.csdn.net/dxflingxing/
2006-5-25 09:34 PM#9
查看资料  访问主页  Blog  发短消息  QQ  顶部
 
dxf218
注册会员
Rank: 2
一般会员



UID 24460
精华 0
积分 155
帖子 201
金钱 155 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2003-11-8
来自 北京
状态 离线
[推荐阅读] 我是新手,请教关于网页刷新的问题


QUOTE:
原帖由 mzsh 于 2006-5-25 08:25 PM 发表
function get_cache_content() {
        $contents = ob_get_contents();
        Return '<!--'.date('Y-m-d H:i:s').'-->'."rn".$contents;
    }

为什么要多写一个"rn"进去?
是加的个 换行标志 适合于linux和windows的换行 \ r \ n




‘dxf零星的’BLOG:http://blog.csdn.net/dxflingxing/
2006-5-25 09:35 PM#10
查看资料  访问主页  Blog  发短消息  QQ  顶部
 16  1/2  1  2  > 
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题 | 开通个人空间  


 




Powered by Discuz! 6.1.0  © 2001-2010 Comsenz Inc.
Processed in 0.045274 second(s), 6 queries

(冀ICP备05009913号) 管理员:sadly 邮箱/MSN: sadly@phpx.com QQ:824008(长隐) 清除 Cookies - - Archiver - WAP