• ----:)欢迎访问源码网(:----
    • 首页
    • 博客
    • 学院
    • 下载
    • 论坛
    • 影视
    • 发布源码
    • RSS
    • ITPig
    • 笑话网
    • 百家姓
    • 繁體中文

源码网 - 中国第一源码门户
选择镜像:网通镜像 - 电信主站
  • 首 页
  • 新闻动态
  • 网站运营
  • 网页制作
  • WEB开发
  • 编程开发
  • 图像媒体
  • 操作系统
  • 数据库
  • 服务器
热门搜索 优化 SEO 故事 cms IIS7 MySQL 个人 AdSense 主题推广 | 文章搜索: 高级搜索
会员登录/控制面版您的位置: 学院首页 >> WEB开发 >> PHP 开发 >> PHP代码 >> 详细内容
 

推荐文章

 
 

热点文章

  • 3389远程服务器GHOST的视频教程
  • 天气预报小偷,根据IP自动判断地址
  • 利用纯真QQIP数据库做快速IP归属地查询
  • php在线文本编辑器
  • 实例(Smarty+FCKeditor新闻系统)
  • php里实现汉字转区位码
  • php的字符编码转换工具
  • 对dvbbs.php 全文搜索的完全分析
  • php生成会动的gif图片代码
  • 56.com视频采集接口程序(PHP)
  • PHP获取网卡的MAC地址
  • 用PHP实现文件上传
 
 

相关文章

 
 

百度搜索

 
 

PHP缓存类(特别针对XML)

  • 阅览次数:
  • 文章来源: CP整理
  • 原文作者:
  • 整理日期: 2008-05-07
  • 发表评论
  • 字体大小:
  • 小
  • 中
  • 大

<?php
/*
* Short Description.
* Cache For PHP5 Based On Xml Or Not
* Detail Description
* @version 1.0
* @access public
* @author Xinge(2006.7.31)
* @update 2006.8.1 15:12 By Xinge
*-----------------------------------
* example:
* $cache = new cache(10000,"../Include/Language/","Gb2312","test");
* $cache -> cacheCheck();
* echo date("Y-m-d H:i:s");
* $cache -> caching();
*/
class cache
{
/*** 默认缓存目录 ***/
public $CacheRoot = "../CacheRoot/";
/*** 缓存更新时间秒数,默认为0,则为不缓存 ***/
public $CacheLimitTime = 0;
/*** 默认缓存文件名 ***/
public $CacheFileName = "";
/*** 默认XML存放路径 ***/
private $XmlRoot = "../Files/Xml/";
/*** 默认XML文件名 ***/
public $XmlSourceName = "";
/*** 默认缓存扩展名 ***/
public $CacheFileExt = "php";
/*=======================================
@函数名:__construct
@作 用:构造函数
@参 数:$CacheLimitTime-------缓存更新时间
$CacheRoot------------缓存目录
$CacheFileName--------缓存文件名
$XmlSourceName--------XML数据源文件名
@返 回:无
@备 注:缓存文件名及XML数据源文件名都不需要加后辍名
=======================================*/
function __construct($CacheLimitTime,$CacheRoot,$CacheFileName,$XmlSourceName)
{
if (intval($CacheLimitTime))
{
$this->CacheLimitTime = $CacheLimitTime;
$this->CacheRoot = $CacheRoot;
if ($CacheFileName == "")
{
$this->CacheFileName = $CacheRoot.$this->autoCacheFileName();
}
else
{
$this->CacheFileName = $CacheRoot.$CacheFileName.".".$this->CacheFileExt;
}
$this->XmlSourceName = $this->XmlRoot.$XmlSourceName.".xml";
//print $this->CacheFileName."<br>".$this->XmlSourceName;
//exit;
}
ob_start();
}

/*=======================================
@函数名:cacheCheck
@作 用:检查缓存文件是否在设置更新时间之内($XmlSourceName不为空时则与XML数据源文件最后时间进行比较)
@参 数:无
@返 回:如果在更新时间之内则返回文件内容,反之则返回失败
@备 注:无
=======================================*/
function cacheCheck()
{
if (file_exists($this->CacheFileName))
{
$CacheFileMakeTime = $this->getFileCreateTime($this->CacheFileName);
$CacheTime = $CacheFileMakeTime + $this->CacheLimitTime;
if ($this->XmlSourceName == "") /*** 缓存内容不是来源于XML数据文件 ***/
{
if ($CacheTime > time())
{
print file_get_contents($this->CacheFileName);
ob_end_flush();
exit;
}
}
else /*** 缓存内容是来源于XML数据文件 ***/
{
if (file_exists($this->XmlSourceName))
{
$XmlFileMakeTime = $this->getFileCreateTime($this->XmlSourceName);
}
else
{
showMsg("XML数据源不存在!","-1",0,3000);
exit;
}
if ($CacheTime > time() && $CacheTime > $XmlFileMakeTime)
{
print file_get_contents($this->CacheFileName);
ob_end_flush();
exit;
}
}
}
else
{
return false;
}
}

/*=======================================
@函数名:caching
@作 用:缓存文件或者输出静态
@参 数:$StaticFileName-------静态文件名(含相对路径)
@返 回:无
@备 注:无
=======================================*/
function caching($StaticFileName = "")
{
if ($this->cacheCheck() == false)
{
if ($this->CacheFileName)
{
$CacheContent = ob_get_contents();
//echo $CacheContent;
ob_end_flush();
}
if ($StaticFileName)
{
$this->saveFile($StaticFileName, $CacheContent);
}
if ($this->CacheLimitTime)
{
$this->saveFile($this->CacheFileName,$CacheContent);
}
}
}

/*=======================================
@函数名:clearCache
@作 用:清除缓存文件
@参 数:$FileName-------------指定文件名(含函数),为all时则删除全部,为空时删除本身
@返 回:清除成功返回true,反之返回false
@备 注:无
=======================================*/
function clearCache($FileName = "all")
{
if ($FileName != "all")
{
if ($FileName == "")
{
$FileName = $this->CacheFileName;
}
else
{
$FileName = $this->CacheRoot.$FileName.".".$this->CacheFileExt;
}
if (file_exists($FileName))
{
return @unlink($FileName);
}
else
{
return false;
}
}
else
{
if (is_dir($this->CacheRoot))
{
if ($dir = @opendir($this->CacheRoot))
{
while ($file = @readdir($dir))
{
$check = is_dir($file);
if (!$check)
{
@unlink($this->CacheRoot.$file);
}
}
@closedir($dir);
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}

/*=======================================
@函数名:autoCacheFileName
@作 用:根据当前动态文件生成缓存文件名
@参 数:无
@返 回:返回缓存文件名
@备 注:无
=======================================*/
function autoCacheFileName()
{
return $this->CacheRoot.strtoupper(md5($_SERVER["REQUEST_URI"])).".".$this->CacheFileExt;
}

/*=======================================
@函数名:getFileCreateTime
@作 用:缓存文件建立时间
@参 数:$FileName-------------缓存文件名(含相对路径)
@返 回:文件生成时间秒数,文件不存在返回0
@备 注:无
=======================================*/
function getFileCreateTime($FileName)
{
if (!trim($FileName)) return 0;
if (file_exists($FileName))
{
return intval(filemtime($FileName));
}
else
{
return 0;
}
}

/*=======================================
@函数名:makeDir
@作 用:连续建目录
@参 数:$Dir------------------目录字符串
$Mode-----------------模式
@返 回:成功则返回true,否则返回错误提示信息
@备 注:无
=======================================*/
function makeDir($Dir, $Mode = "0777")
{
if (!$Dir) return 0;
$Dir = str_replace("\\","/",$Dir);
$mDir = "";
foreach(explode("/",$Dir) as $val)
{
$mDir .= $val."/";
if ($val == ".." || $val == ".") continue;
if (!file_exists($mDir))
{
if (!@mkdir($mDir,$Mode))
{
showMsg("创建目录 [".$mDir."] 失败!","-1",0,3000);
exit;
}
}
}
return true;
}

/*=======================================
@函数名:saveFile
@作 用:保存缓存文件
@参 数:$FileName-------------缓存文件名(含相对路径)
$text-----------------缓存内容
@返 回:成功返回ture,失败返回false
@备 注:无
=======================================*/
function saveFile($FileName,$text)
{
if (!$FileName || !$text) return false;
if ($this->makeDir(dirname($FileName)))
{
if ($fp = fopen($FileName,"w"))
{
if (@fwrite($fp,$text))
{
fclose($fp);
return true;
}
else
{
fclose($fp);
return false;
}
}
}
return false;
}
}
?>

上一篇:PHP生成静态页面详解
下一篇:微软将下一个收购目标定为美国在线
  • 网友评论:
  • 查看所有评论
  • 我要发表评论
您的网名:
留言主题:
你要发表的内容:

 

关于本站 | 广告联系 | 版权声明 | 网站地图 | 发布软件 | 帮助中心 | 源码论坛

Copyright © 2005-2007 CodePub.Com  程序支持:木翼  滇ICP备05005971号