基于phplib template的缓存类[R]

本类基于phplib template类.


<?php
/*
 * cacheTemplate writed by 风随影动Surrey(surreyk@msn.com)
 * Programming Language PHP5
 * $Id: phplib_template.class.php,v 1.0 2005/03/05 08:51:35 kk Exp $
 *
 */
/********************cacheTempalte类*********************************/
class cacheTemplate extends Template
{
/* cache的目录,请保证0777权限*/
protected static $cacheDir = "./cache";

/* cache超时时间,单位秒*/
protected static $timeout  = 60;

/*构造函数,使用方法与template类相同*/
public function __construct($root = ".", $unknowns = "remove")
{
$this->Template($root, $remove);
}
             /* 设置文件,uniqID为此文件唯一ID,手动指定,请保证同一文件的ID相同*/
public function setFile($handle, $filename, $uniqID)
{
if (!is_array($handle)) {
      if ($filename == "") {
        throw new templateException("set_file: For handle $handle filename is empty.", 60001);
        return false;
      }
      $this->file[$handle] = $this->filename($filename);
    } else {
      reset($handle);
      while(list($h, $f) = each($handle)) {
        $this->file[$h] = $this->filename($f);
      }
    }
    $this->uniqID = $uniqID;
ob_start();
}
/*判断uniqID是否已经被缓存,调用方法 cacheTemplate::isCached(uniqID)*/
public function isCached($uniqID)
{
//return false;
$cacheFile = self::$cacheDir."/".md5($uniqID);
if (file_exists($cacheFile) &&
date("YmdHis") - date("YmdHis", filemtime($cacheFile)) < self::$timeout) {
return true;
} else {
return false;
}
}
/*刷新缓存,即删除uniqID对应文件,调用方法$cacheTemplate->refresh(uniqID)*/
public function refresh($uniqID)
{
    if ($this->isCached($uniqID)) {
        @unlink($this->cacheDir."/".md5($uniqID));
        return true;
    }
    return false;
}
/*输出缓存内容,调用方法cacheTemplate::printCache(uniqID)*/
public function printCache($uniqID)
{
    $cacheFile = self::$cacheDir."/".md5($uniqID);
    if (self::isCached($uniqID)) {
        $fhandle = @fopen($cacheFile, "rb");
        if ($fhandle) {
            while(!feof($fhandle)) {
                print fgets($fhandle,1024);
            }
            fclose($fhandle);
            return true;
        }
    }
    return false;
}
/*输出模板解析后内容,请务必使用这个函数而不是template中的parse()然后p()*/
public function pparse($target, $handle, $append = false)
{
print $this->parse($target, $handle, $append);
$content = ob_get_contents();
ob_end_flush();
$fhandle = @fopen(self::$cacheDir."/".md5($this->uniqID), "wb");
if ($fhandle) {
fwrite($fhandle, $content);
fclose($fhandle);
}
$this->uniqID = null;
}
}
?>

AddThis Social Bookmark Button

相关文档(Relevant Entries)
php 常用组件等整理
在mysql中求日期差
简单的PHP重定向代码
Andreessen: PHP succeeding where Java isn't
PHPLIB模板嵌套示例
用Zend Encode编写开发PHP程序
如何编译PHP源代码
Zend Optimizer 配置指南
Posted on July 26, 2006 10:11 PM | | | Comments (0) | | TrackBacks (0)

引用地址(TRACKBACKS)
 
TrackBack URL for this entry:
http://www.wujianrong.com/mt-tb.cgi/914

发布评论(ADD YOUR COMMENTS)
 
感谢您参与评论;发表您的意见时请保持文章的相关性;不相关的或是单纯宣传的内容可能会被删掉。您的E-mail只是用来确认您发表的文章,不会出现在网页上。
Please keep your comments relevant to this blog entry. Email addresses are never displayed, but they are required to confirm your comments.

称呼(Name):      记住我的个人信息(Remember)
邮箱(Email):
网址(Website):
评论(Add your comments):

相关内容