首页 > Web开发 > 详细

php简单数据缓存类

时间:2015-06-02 13:19:16      阅读:177      评论:0      收藏:0      [点我收藏+]

公司手机触屏站 ,由于页面图片太多,所以需要做数据缓存,就随便写一个数据缓存类。

直接贴代码

<?php
/**
*
* fianl_m@foxmail.com
* 缓存类
* 把数据查询出,并序列化写入文件
**/
class Cache{

function __construct($config){
//定义是否开启缓存
$this->is_cache=$config[‘is_cache‘];
//定义缓存目录
$this->cache_file=$config[‘cache_file‘];
//定义缓存时间
$this->cache_time=$config[‘cache_time‘];


}

//读取缓存文件
public function open($name){

$arr=array();
$filename=$this->cache_file.$name;
$status=filemtime($filename)+$this->cache_time>time();//定义缓存时间
if( file_exists($filename) && $status && $this->is_cache){
$content=file_get_contents($filename);//读取缓存文件
$arr=unserialize($content);
return $arr;
}else{
return false;
}

}
//写入缓存文件
public function write($name,$data=array()){
$filename=$this->cache_file.$name;
$content=serialize($data);
file_put_contents($filename, $content);//写入缓存文件

}

 

 

}


?>

 

其实无非就是,把select的数组  然后序列化 放进文本中 然后读出来。

使用方法

//定义缓存是否开启
require(‘cache.class.php‘);
$config=array(
‘is_cache‘=>1,//是否开启缓存
‘cache_file‘=>‘./cache/‘,//缓存文件夹
‘cache_time‘=>‘60‘,//缓存时间
);
$cache=new Cache($config);

//打开缓存,传入缓存文件名字

$row=$cache->open($filename);

//写入缓存传入文件名字  和数据(数组)

$cache->write($filename,$data);

 

php简单数据缓存类

原文:http://www.cnblogs.com/hywl/p/4545971.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!