查看: 4779|回复: 0
打印 上一主题 下一主题

php缓存类

[复制链接]
跳转到指定楼层
1#
发表于 2007-12-12 19:03:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
<?
/*
程序设计: 林建炫(飘枫设计室)
made in 陆丰 by 2007-03-03 中午
QQ: 5818500
Email: linzhenxuan@163.com
尊重他人也尊重自己!请勿删除该版权信息
如果你使用、修改请保留该信息!
类名称: 缓存使用类
用途: 全局变量、模版
不足地方: 性能没内存共享快!
*/
Class Cls_cache
{
var $dir='cache';//缓存目录
var $auto = true;//是否自动删除过时缓寸,如果缓存文件 大、多 不建议这样做
var $file_list;//文件名
var $dir_name;//目录绝对路径;
var $code = "<?php exit;?>";//加入的代码
var $cache_time = "3600";//缓存时间,默认1小时
Function __construct()
{
//class start;
//建立目录
$dir = $this->dir;
$dir = explode("/",$dir);
foreach($dir as $a)
{
if($i==0)
{
$temp .= $a;
}
else
{
$temp .= "/".$a;
}
if(!is_dir($temp))
{
mkdir($temp);
};
$i++;
}
//自动清除过期缓存文件
if($this->auto==true){$this->auto();}
//读取目录文件
$this->read_file();
}
Function read_file()
{//得到格式 fil1|fil2|fil3|fil4....
$dir_name = realpath($this->dir);
$this->dir_name = $dir_name;
$dir = opendir($dir_name);//打开目录
while ($file_name = readdir($dir))
{
if(($file_name!=".")&&($file_name!=".."))
{
$file_list .=$file_name."|";
}
}
$file_list = substr($file_list,0,(strlen($file_list)-1));
closedir($dir);
$this->file_list = $file_list;
return $file_list;
}
Function read($strname)
{//read('index.php?page=1');
$strname = md5(trim($strname));
$list = $this->file_list;
$re = "/(".$strname.")_([0-9]*)_([0-9]*)[a-zA-Z0-9\.]*/i";
if(preg_match($re,$list,$match))
{//找到文件
$file = $this->dir_name."\\".$match[0];
if($match[2]>$match[3]){unlink($file);return false;}
$content = file_get_contents($file);
$content = substr($content,strlen($this->code),(strlen($content)-strlen($this->code)));
return $content;
}
}
Function write($strname,$content,$cache_time='')
{
$strname = md5(trim($strname));
$list = $this->file_list;
$time = explode(" ",microtime());
$time = $time[1];
if(!$cache_time){$cache_time = $this->cache_time;}
$cache_time += $time;
$re = "/(".$strname.")_([0-9]*)_([0-9]*)[a-zA-Z0-9\.]*/i";
if(preg_match($re,$list,$match))
{//找到文件
$re_file = $this->dir_name."\\".$strname."_".$time."_".$cache_time.".php";
$file = $this->dir_name."\\".$match[0];
rename($file,$re_file);
$content = $this->code.$content;
$content = file_put_contents($re_file,$content);
$this->read_file();
return true;
}
else
{
$file = $this->dir_name."\\".$strname."_".$time."_".$cache_time.".php";
$content = $this->code.$content;
file_put_contents($file,$content);
$this->read_file();
}
}
Function delete($strname)
{
$strname = md5(trim($strname));
$list = $this->file_list;
$re = "/(".$strname.")_([0-9]*)_([0-9]*)[a-zA-Z0-9\.]*/i";
if(preg_match($re,$list,$match))
{//找到文件
$file = $this->dir_name."\\".$match[0];
unlink($file);
$this->read_file();
return true;
}else{return false;}
}
Function auto()
{//功能自动清除过期缓存文件
$list = $this->read_file();
$re = "/([a-zA-Z0-9]*)_([0-9]*)_([0-9]*)[a-zA-Z0-9\.]*/i";
if(preg_match_all($re,$list,$match))
{
for($i=0;$i<=(count($match[0])-1);$i++)
{
$file = $this->dir_name."\\".$match[0][$i];
if($match[2][$i]>$match[3][$i]){@unlink($file);$this->read_file();}
}
}
}
}
//测试代码
$cache = New Cls_cache;
$list = $cache->file_list;
//$cache->write('content','我是最好的哈哈');
echo $cache->read('content');
?>
这是我刚完成的缓存类!准备用在论坛的缓存!
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表