A-A+

php简单的数据缓存类

2014年06月15日 PHP技术文章 评论 2 条 阅读 202 views 次

在php中很容易使用缓存技术,使用缓存的目的就是要减轻服务器的压力,减轻服务器的压力也可以可以使用生成静态,但如果网站的文章数量过于庞大,有成千上万条的数据,不仅会占用很大的服务器空间,而且管理起来也不容易,这个时候就可以使用缓存技术了。

网页缓存可以分为整体页面的缓存和局部缓存,整体页面的缓存就是将整个页面进行缓存,而局部缓存是将页面的一部分进行缓存,比如经常变动的部分,除了整体页面和局部缓存外,当然还可以分为数据缓存等等。。

缓存的作用可以让访客在访问数据库读取数据之前先进行读取缓存的文件,如果文件没有更改过,则直接读取缓存的文件,而不用再去调用数据库,因而大大减轻了服务器的压力,在php中实现缓存是非常简单的,通常都是用缓存类来实现的,缓存类中应用了大量的文本操作类,主要用来读取缓存文件来使用的。

下面是一个简单的数据缓存操作类,可以实现建立缓存目录,以及开启是否自动删除过时缓存,缓存时间,等操作。另外给出测试代码,使用时,需要先建立 cache 总的缓存目录。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>简单数据缓存操作类</title>
</head>
<body>
<?php
/**
author:smiling
web:www.xiariboke.net
date:2010/04/18
*/
error_reporting(E_ERROR|E_PARSE|E_WARNING);
date_default_timezone_set("Asia/Shanghai");
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 = strtotime('now');
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]){
echo $file;
@unlink($file);
$this->read_file();
}
*/

if(strtotime('now') > $match[3][$i]){
@unlink($file);
$this->read_file();
}
}
}
}
}
//测试代码
$cache = new Cls_cache();
$list = $cache->file_list;
$cache->auto();
if($cache->read('content')==null){
$cache->write('content','array(1,2,3,4,5)大西瓜'.date('Y-m-d H:i:s'));
}
echo $cache->read('content');
//$cache->delete('content');
?>
</body>
</html>

小提示:可以直接进行运行,在实际运用时,可以将其 $cache->write('content','array(1,2,3,4,5)大西瓜'.date('Y-m-d H:i:s')); 中的内容更换为数据库中的读取内容或其它。

标签:

2 条留言  访客:2 条  博主:0 条

  1. 源码窝

    你网站一更新的呀,挺勤快的,加油

  2. 57博客

    前来支持~~~~~~~~~~~~~~~~~~~
    谢谢博主,,,收藏ing

给我留言