GNU IS NOT UNIX
贴一个Cache类个人用得不错
上一篇 /
下一篇 2007-12-27 14:48:49
/ 个人分类:杂
<?php
class cache
{
var $cacheDirectory;
var $cacheDuration;
var $cacheFilename;
function cache($cacheDuration=3600,$cacheDirectory='./cache')
{
$this->cacheDuration = 0;
$this->cacheFilename = '';
$this->cacheDirectory = '.';
$this->updateCache($cacheDuration,$cacheDirectory);
}
function _makeCacheFolder()
{
if (!is_dir($this->cacheDirectory))
{
$temp = explode('/',$this->cacheDirectory);
$cur_dir = '';
for($i=0;$i<count($temp);$i++)
{
$cur_dir .= $temp[$i].'/';
if (!is_dir($cur_dir))
{
if (@mkdir($cur_dir)&&($cur_dir!=getcwd()))
{
$this->_writeFile($cur_dir.'.htaccess','Deny from all');
$this->_writeFile($cur_dir.'index.html','');
}
}
}
}
}
function getCacheFilename()
{
return $this->cacheFilename;
}
function _setCacheFilename($contents)
{
$this->cacheFilename = $this->cacheDirectory.'/'.md5($contents).'.txt';
}
function inCache($contents)
{
$this->_setCacheFilename($contents);
return file_exists($this->cacheFilename);
}
function readCache()
{
$contents = '';
$fp = @fopen($this->cacheFilename,'r');
if ($fp)
{
while(!feof($fp)) $contents .= fread($fp,4096);
fclose($fp);
}
return $contents;
}
function updateCache($cacheDuration=86400,$cacheFolder='./cache')
{
$this->cacheDuration = $cacheDuration;
$this->cacheDirectory = $cacheFolder;
$this->_makeCacheFolder();
}
function saveInCache($contents,$filename='')
{
if (trim($filename)=='') $filename = $contents;
if ($this->inCache($filename)&&((filectime($this->cacheFilename)-time())>$this->cacheDuration))
{
@unlink($this->cacheFilename);
}
$this->_writeFile($this->cacheFilename,$contents);
}
function _writeFile($filename,$contents)
{
if (!file_exists($filename))
{
$fp = @fopen($filename,'w');
if ($fp)
{
fputs($fp,$contents);
fclose($fp);
}
}
}
}
?> <?php
使用方法:
include_once(_CONPATH_."Class/Class_Cache.php");
$cache=new cache();
if($cache->incache($_SERVER['QUERY_STRING']))
{
$output=$cache->readcache();
}
else
{
ob_start();
//这里是输出的内容
$output = ob_get_clean();
$cache->saveInCache($output,$_SERVER['QUERY_STRING']);
}
unset($cache);
print($output);
exit;
?>
相关阅读:
- 理解MySQL数据类型,mysql数据库 (Ajax_chou, 2007-12-26)
- MySQL数据库新建用户,授权,删除用户,修改密码等操作 (Ajax_chou, 2007-12-26)
- 微软不断增强对开源编程语言PHP的支持 (tomsou, 2007-12-27)
- LINUX虚拟主机的好处与PHP的完美组合 (tomsou, 2007-12-27)
- MySpace Poll Creator脚本index.php漏洞 (tomsou, 2007-12-27)
- Beehive论坛post.php脚本远程SQL注入漏洞 (tomsou, 2007-12-27)
- FrontAccounting 存在远程文件包含漏洞 (tomsou, 2007-12-27)
- WordPress wp-db.php文件字符集SQL注入漏洞 (tomsou, 2007-12-27)
- 国际:PHP糟糕的状态还在延续 (tomsou, 2007-12-27)
- 新手入门全面介绍MySQL基础应用 (大龄青年, 2007-12-27)
导入论坛
收藏
分享给好友
推荐到圈子
管理
举报
TAG:
php
cache