// +----------------------------------------------------------------------

// |缓存类

// +----------------------------------------------------------------------

// | Author: justmepzy(justmepzy@gmail.com)

// +----------------------------------------------------------------------

class Cache{

//提示信息

public $tip_message;

//缓存目录

protected $cache_dir;

//缓存文件名

private $cache_file_name;

//缓存文件后缀

private $cache_file_suffix;

public function __construct($dir,$cache_file_suffix ='.php'){

$this->cache_dir = isset($dir)?$dir:dirname(__FILE__).DIRECTORY_SEPARATOR.'default_cache_data';

$this->cache_file_suffix =$cache_file_suffix;

if(!$this->dir_isvalid($this->cache_dir)){

die($this->tip_message);//创建目录失败

}

}

// +----------------------------------------------------------------------

// |添加一个值,如果已经存在,则返回false,写入文件失败返回false

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

public function add($cache_key,$cache_value,$life_time=1800){

if(file_exists($this->get_cache_file_name($cache_key))){

$this->tip_message ='缓存数据已存在.';

return false;

}

$cache_data['data'] =$cache_value;

$cache_data['life_time'] =$life_time;

//以JSON格式写入文件

if(file_put_contents($this->get_cache_file_name($cache_key), json_encode($cache_data))){

return true;

}else{

$this->tip_message ='写入缓存失败.';

return false;

}

}

// +----------------------------------------------------------------------

// |添加一个值,如果已经存在,则覆写

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

public function set($cache_key,$cache_value,$life_time=1800){

$cache_data['data'] =$cache_value;

$cache_data['life_time'] =$life_time;

if(file_put_contents($this->get_cache_file_name($cache_key), json_encode($cache_data))){

return true;

}else{

$this->tip_message ='写入缓存失败.';

return false;

}

}

// +----------------------------------------------------------------------

// |获取一个key值

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

public function get($cache_key){

if(!file_exists($this->get_cache_file_name($cache_key))){

return false;

}

$data =$this->object_to_array(json_decode(file_get_contents($this->get_cache_file_name($cache_key))));

if($this->check_isvalid($data['life_time'])){

unset($data['life_time']);

return $data['data'];

}else{

unlink($this->cache_file_name);

$this->tip_message ='数据已过期.';

return false;

}

}

// +----------------------------------------------------------------------

// |删除一个key值

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

public function delete($cache_key){

if(file_exists($this->get_cache_file_name($cache_key))){

if(unlink($this->get_cache_file_name($cache_key)))

return true;

else

return false;

}else{

$this->tip_message ='文件不存在.';

return true;

}

}

// +----------------------------------------------------------------------

// |清除所有缓存文件

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

public function flush(){

$this->delete_file($this->cache_dir);

}

// +----------------------------------------------------------------------

// |自动清除过期文件

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

public function auto_delete_expired_file(){

$this->delete_file($this->cache_dir,false);

}

// +----------------------------------------------------------------------

// |检查目录是否存在,不存在则创建

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

private function dir_isvalid($dir){

if (is_dir($dir))

return true;

try {

mkdir($dir,0777);

}catch (Exception$e) {

$this->tip_message ='所设定缓存目录不存在并且创建失败!请检查目录权限!';

return false;

}

return true;

}

// +----------------------------------------------------------------------

// |检查有效时间

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

private function check_isvalid($expired_time = 0) {

//if(!file_exists($this->cache_file_name)) return false;

if (!(@$mtime =filemtime($this->cache_file_name)))return false;

if (time() -$mtime >$expired_time)return false;

return true;

}

// +----------------------------------------------------------------------

// |获得缓存文件名

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

private function get_cache_file_name($key){

$this->cache_file_name =$this->cache_dir.DIRECTORY_SEPARATOR.md5($key).$this->cache_file_suffix;

return $this->cache_file_name;

}

// +----------------------------------------------------------------------

// |object对象转换为数组

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

protected function object_to_array($obj){

$_arr =is_object($obj) ? get_object_vars($obj) :$obj;

foreach ($_arr as $key =>$val) {

$val = (is_array($val) ||is_object($val)) ? object_to_array($val) :$val;

$arr[$key] =$val;

}

return $arr;

}

// +----------------------------------------------------------------------

// |删除目录下的所有文件

// +----------------------------------------------------------------------

// | $mode true删除所有 false删除过期

// +----------------------------------------------------------------------

protected function delete_file($dir,$mode=true) {

$dh=opendir($dir);

while ($file=readdir($dh)) {

if($file!="." &&$file!="..") {

$fullpath=$dir."/".$file;

if(!is_dir($fullpath)) {

if($mode){

unlink($fullpath);

}else{

$this->cache_file_name =$fullpath;

if(!$this->get_isvalid_by_path($fullpath)) unlink($fullpath);

}

}else {

delete_file($fullpath,$mode);

}

}

}

closedir($dh);

}

private function get_isvalid_by_path($path){

$data =$this->object_to_array(json_decode(file_get_contents($path)));

return $this->check_isvalid($data['life_time']);

}

}

?>

php缓存类,PHP缓存类相关推荐

  1. 【EventBus】事件通信框架 ( 订阅方法注册 | 检查订阅方法缓存 | 反射获取订阅类中的订阅方法 )

    文章目录 一.检查订阅方法缓存 二.反射获取订阅类中的订阅方法 三.完整代码示例 一.检查订阅方法缓存 注册订阅者时 , 只传入一个订阅者类对象 , 其它信息都需要通过反射获取 ; 1. 获取订阅者类 ...

  2. android 缓存文件的工具类,总结的一些android公共库,包含缓存(图片缓存、预取缓存)、...

    总结的一些android公共库,包含缓存(图片缓存.预取缓存).公共View(下拉及底部加载更多ListView.底部加载更多ScrollView.滑动一页Gallery).及工具类(下载管理.静默安 ...

  3. php mysql文件缓存_PHP文件缓存类实现代码

    php中缓存分类数据库缓存,文件缓存和内存缓存,下面我来给各位同学详细介绍PHP文件缓存类实现代码,有需要了解的朋友可参考. 页面缓存类 代码如下 : /*include( "cache.p ...

  4. LindAgile~缓存拦截器支持类的虚方法了

    写它的原因 之前写过一个缓存拦截器,主要在方法上添加CachingAspect特性之后,它的返回值就可以被缓存下来,下次访问时直接从缓存中返回结果,而它有一个前提,就是你的方法需要是一个接口方法,缓存 ...

  5. 漫话:如何给女朋友解释什么是缓存穿透、缓存击穿、缓存雪崩?

    作者 | 漫话编程 来源 | 漫话编程(ID:mhcoding) 周末在家面试,和候选人聊到Redis的问题,于是问了他一个问题:你知道缓存穿透.缓存击穿和缓存雪崩吗?他们之间的区别是什么?分别怎么解 ...

  6. hibernate之 一级缓存和二级缓存

    2019独角兽企业重金招聘Python工程师标准>>> 缓存 缓存的实现不仅需要作为物理介质的硬件,同时需要管理缓存的并发访问策略和过期策略的程序(软件).所以缓存通常是通过软件和硬 ...

  7. Hibernate二级缓存与查询缓存的组合探究

    0.前言 由于对Hibernate的二级缓存和查询缓存的区别不了解,也不知道它们起什么作用.于是动手做了一些实验,对它们的组合使用有了一个表面的认识. 1.前提 1) 不使用一级缓存(Session级 ...

  8. java spring boot缓存_Springboot对缓存的支持

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 缓存是依赖于org.springframework.cache.Cache和 org.springframework.cache.CacheManager ...

  9. 图片缓存之内存缓存技术LruCache,软引用

    图片缓存之内存缓存技术LruCache,软引用 每当碰到一些大图片的时候,我们如果不对图片进行处理就会报OOM异常, 这个 问题曾经让我觉得很烦恼 ,后来终于得到了解决, 那么现在就让我和大家一起分享 ...

  10. iOS SDWebImage 缓存机制与缓存策略

    2019独角兽企业重金招聘Python工程师标准>>> 一.SDWebImage 缓存机制 1.基本用法 SDWebImage提供一个UIImageView的Category,用来加 ...

最新文章

  1. 如何给6个整数的一维数组某个元素赋值_数组指针详解
  2. Computer:计算机测试理论(开发/测试/上线)之DEV、SIT、UAT、PRD四套环境详细介绍之详细攻略
  3. BZOJ2169 连边(动态规划)
  4. 阿里云服务器怎么去掉tomcat的8080端口
  5. 智能数据引擎Dataphin重磅发布,提供一站式的技术管理能力
  6. Android Studio 设置/更改 SDK 路径
  7. 集腋成裘-13-git使用-02进阶篇
  8. 新科LoRa网关和LoRa节点
  9. ArcGIS 实验理论基础六 ArcCatalog中空间数据的操作
  10. 每日学习笔记(12)
  11. win10应用商店打不开_为何win10应用商店不能下载itunes
  12. Pyton入门的歪路
  13. java怎么绘画坦克_坦克游戏教程一:使用java绘图功能绘制简单坦克
  14. 基于c#的区块链编程_3.区块链 · C#区块链编程入门教程-巴比特图书
  15. UVa:10105 Polynomial Coefficients(多项式定理)
  16. java排查full gc_一次频繁Full GC问题排查过程分享
  17. Halcon九点及旋转标定流程
  18. 怎么去掉视频上的水印?快速去除水印或字幕的大神技巧
  19. python 如何爬虫wind api数据_Python网络爬虫实战之十:利用API进行数据采集
  20. Linux中软件管理的yum命令

热门文章

  1. win7下安装pip——Python的包管理工具
  2. python 函数、面向对象
  3. 面试中的常见14种算法套路
  4. 华为手机打字声音怎么开启_华为手机这5个超实用小功能,记得要开启,谁用都说好...
  5. HB-X打不开的解决办法
  6. php 文章读取_php实现获取文章内容第一张图片的方法
  7. 将url参数字符串转成数组
  8. vue路由跳转报错解决
  9. setTimeout和setInterval的区别
  10. 可视化分析js的内存分配与回收