PHP缓存操作类
2013-10-31 10:22
302 查看
1,cache.class.php缓存操作类
2,缓存类的使用cache.php
<?php /** +---------------------------------------------------------- * franklin 缓存操作类 +---------------------------------------------------------- * 文件名称 cache.class.php +---------------------------------------------------------- * 文件描述 缓存操作类 +---------------------------------------------------------- * 作 者 +---------------------------------------------------------- * 时 间 2012-05-05 +---------------------------------------------------------- **/ class cache{ //查询参数 protected $options=array(); //缓存次数 protected $cacheCount=0; //缓存时间 protected $cachetime=60; //缓存路径 protected $cachePath='cache/'; //数据返回类型, 1代表数组, 2代表对象 protected $returnType=1; /** * 读取缓存 * @param string $id 缓存名称 * @param int $time 缓存有效时间,默认为60秒,0为永远失效 * @param string $path缓存文件存放路径 * @accesspublic readCache * @returnmixed如果读取成功返回缓存内容, 否则返回NULL **/ public function readCache($id,$time,$Path=''){ $id=md5($id); $this->cachePath=empty($Path)?$this->cachePath:$Path; $this->cachetime=empty($time)?$this->cachetime:$time; $file=$this->cachePath.$id.'.php'; if(file_exists($file)){ //缓存过期 if((filemtime($file)+$time)<time()){ @unlink($file); return NULL; } if(1===$this->returnType){ $row=include $file; }else{ $data=file_get_contents($file); $row=unserialize($data); //取出数据,储存的资料以byte-stream方式存放。返回值为混合类型,包括整数、倍精确浮点数字符串、数组以及类的属性。 } return $row; } return NULL; } /** * 写入缓存 * * @accesspublic * @param mixed$data缓存内容 * @returnbool是否写入成功 **/ public function writeCache($id,$data,$Path=''){ $this->cacheCount++; $id=md5($id); $this->cachePath=empty($Path)?$this->cachePath:$Path; $file=$this->cachePath.$id.'.php'; chmod($this->cachePath,777); if(1===$this->returnType){ $data='<?php return '.var_export($data, TRUE).'; ?>'; //var_export()函数返回关于传递给该函数的变量的结构信息. }else{ $data=serialize($data); //以byte-stream方式储存资料到系统中,变量 value 为混合类型,包括整数、倍精确浮点数字符串、数组以及类的属性. } return file_put_contents($file, $data); } /** * 删除指定缓存 * * @accesspublic * @param mixed$id缓存名称 * @returnbool是否删除成功 **/ public function delCache($id,$Path=''){ $id=md5($id); $this->cachePath=empty($Path)?$this->cachePath:$Path; $file=$this->cachePath.$id.'.php'; if(file_exists($file)){ return unlink($file); } return NULL; } /** * 删除所有缓存 * * @accesspublic * @param mixed$dir缓存名称 * @returnbool清除所有缓存是否成功 **/ public function delAllCache($Path=''){ $id=md5($id); $this->cachePath=empty($Path)?$this->cachePath:$Path; $path=$this->cachePath; if(is_dir($path)){ if($dh=opendir($path)){ while(($file=readdir($dh))!==false){ if($file!='..'&$file!='.'){ if(is_dir($path.'/'.$file)){ if(!delDir($path.'/'.$file)){ return 0; } }else{ if(!unlink($path.'/'.$file)){ return 0; } } } } closedir($dh); } return 1; } } }
2,缓存类的使用cache.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <?php include('cache.class.php'); $data=array('http://www.phpernote.com','http://www.baidu.com','http://www.google.cn'); $cache=new cache(); $id='test'; //写入缓存 $cache->writeCache($id,$data); //读缓存并打印 $name_row=$cache->readCache($id,120); print_r($name_row); //删除某个变量 $cache->delCache($id); //删除全部缓存 $cache->delAllCache(); ?> </body> </html>
相关文章推荐
- 简单的php缓存类分享 php缓存机制
- PHP 文件缓存数组的实现
- php 数据库操作类
- PHP数据缓存技术
- PHP学习 --smarty 过滤器 缓存等
- PHP扩展--APC缓存安装与使用
- PHP redis操作类 个人总结
- php 清除缓存代码
- php实现监控varnish缓存服务器的状态
- php操作Memcached缓存系统
- PHP扩展缓存加速安装
- PHP输出缓存ob系列函数详解
- PHP日期操作类代码-农历-阳历转换、闰年、计算天数等
- PHP的文件缓存类
- php缓存
- dedecms中提取的zip压缩文件操作类zip.class.php
- PHP-编译运行过程以及opcode缓存的笔记
- 让浏览器提高缓存效率,使用HTTP_IF_MODIFIED_SINCE缓存提高PHPWEB性能
- PHP 文件操作类(创建文件并写入) 生成日志
- PHP一个小巧的缓存类