php缓存类,PHP缓存类

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

// |缓存类

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

// | 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']);

}

}

?>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/369584.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

双向@OneToMany / @ManyToOne关联

编程的目标之一是代表现实世界中的模型。 通常,应用程序需要对实体之间的某些关系进行建模。 在上一篇有关Hibernate关联的文章中,我描述了建立“一对一”关系的规则。 今天,我将向您展示如何设置双向的“ 一对多 ”和“ 多对一 ”关联。 这个…

web前端黑客技术揭秘 6.漏洞挖掘

6.1  普通XSS漏洞自动化挖掘思路 6.1.1  URL上的玄机 6.1.2  HTML中的玄机 2.HTML标签之内 6.1.3  请求中的玄机 6.1.4  关于存储型XSS挖掘 6.2.1  HTML与JavaScript自解码机制 <input type"button" id"exec_btn" value"exec" on…

Webpack基础使用

目录 一.什么是Webpack 二.为什么要使用Webpack 三.Webpack的使用 1.下载yarn包管理器 2.Webpack的安装 3.Webpack的简单使用 4.效果 四.Webpack打包流程 一.什么是Webpack Webpack是一个静态模块打包工具 二.为什么要使用Webpack 在开发中&#xff0c;我们常常会遇到…

CSS3及JS媒体查询教程

CSS3媒体查询&#xff1a; 语法&#xff1a; <media_query_list>&#xff1a;<media_query>[,<media_query>] <media_query>&#xff1a;only|not <mediaType> and <expression>[ and <expression>] <expression>&#xff1a;…

php mongodb

// 欄位字串為$querys array("name">"shian");// 數值等於多少$querys array("number">7);// 數值大於多少$querys array("number">array($gt > 5));// 數值大於等於多少$querys array("number">array($…

阿帕奇骆驼遇见Redis

键值商店的兰博基尼 Camel是最好的面包集成框架&#xff0c;在本文中&#xff0c;我将向您展示如何通过利用另一个出色的项目Redis使它更加强大。 Camel 2.11即将发布&#xff0c;具有许多新功能&#xff0c;错误修复和组件。 这些新组件中的几个是我创作的&#xff0c; red…

php 数字加逗号,php数字满三位添加一逗号

//数字满三位添加一逗号&#xff1a;$s_money1 1000000;$s_money2 number_format($s_money1);echo $s_money1;//1000000echo "";echo $s_money2;//1,000,000PHP number_format() 函数number_format() 函数通过千位分组来格式化数字。注释&#xff1a;该函数支持一个…

HTML 教程- (HTML5 标准)摘抄笔记

HTML 教程- (HTML5 标准) 教程网址&#xff1a;http://www.runoob.com/html/html-tutorial.html http://blog.csdn.net/ljfbest/article/details/6700148 HTML版本 从初期的网络诞生后&#xff0c;已经出现了许多HTML版本: 版本发布时间HTML1991HTML 1993HTML 2.01995HTML 3…

spring 隔离级别 测试代码

Controller RequestMapping("/test") Api(value "测试", description "测试") public class TestController {Autowiredprivate TestService testService;RequestMapping(value "listForDirtyRead", method RequestMethod.GET)Res…

他人的一些2017年度总结

闭环思维&#xff1a;自己在做工作的时候&#xff0c;以及在做事情的时候&#xff0c;逐渐养成了闭环思考模式。一个新的东西&#xff0c;一个新的方案&#xff0c;从场景开始梳理&#xff0c;一步步的梳理流程和方案&#xff0c;然后到最终的方案落地&#xff0c;所有的一套流…

Java EE CDI ConversationScoped示例

在本教程中&#xff0c;我们将向您展示如何在Web应用程序中创建和使用ConversationScoped Bean。 在CDI中&#xff0c;bean是定义应用程序状态和/或逻辑的上下文对象的源。 如果容器可以根据CDI规范中定义的生命周期上下文模型来管理其实例的生命周期&#xff0c;则Java EE组件…

block与inline,inline和inline-block,块级和行内元素,行内替换和行内非替换元素

block:块级元素默认display属性为block&#xff1b;无论块内内容有多少&#xff0c;总是占满一行&#xff1b; inline:行内元素默认display属性为inline&#xff1b;只占据块内的内容的大小&#xff0c;不会占满一整行&#xff1b; inline-block:将行内元素的display属性改为…

mybatis深入理解之 # 与 $ 区别以及 sql 预编译

mybatis 中使用 sqlMap 进行 sql 查询时&#xff0c;经常需要动态传递参数&#xff0c;例如我们需要根据用户的姓名来筛选用户时&#xff0c;sql 如下&#xff1a; select * from user where name "ruhua"; 上述 sql 中&#xff0c;我们希望 name 后的参数 "ru…

Git 中文教程

以下内容转载自&#xff1a;http://www.open-open.com/lib/view/open1328928294702.html Git是一个分布式的版本控制工具&#xff0c;本篇文章从介绍Git开始&#xff0c;重点 在于介绍Git的基本命令和使用技巧&#xff0c;让你尝试使用Git的同时&#xff0c;体验到原来一个版本…

php markdown的转化函数,markdown公式转为知乎格式

在知乎中写技术类文章&#xff0c;经常会用到markdown知乎文章可以导入markdown格式&#xff0c;但是不支持Latex公式。知乎大神提供了替代方案&#xff1a; https://zhuanlan.zhihu.com/p/69142198为实现自动化&#xff0c;用python将其简易实现&#xff0c;代码如下&#xff…

Java EE CDI Producer方法教程

这是CDI Producer方法的教程。 在CDI中&#xff0c;生产者方法生成一个可以注入的对象。 当我们要注入本身不是bean的对象&#xff0c;要注入的对象的具体类型在运行时可能有所不同&#xff0c;或者当对象需要一些bean构造函数不执行的自定义初始化时&#xff0c;可以使用生产者…

CSS的常用属性(二)

盒子模型之边框 border-(top/bottom/left/right)-style: solid 边框的风格 如(solid 实线&#xff0c;dotted 点线&#xff0c;dashed 虚线) border-top-color: #aaa 边框颜色 border-top-width: 20px 边框粗细 边框四个方向连写&#xff1a; 如 border-color: #000 边框属…

牛客网Java刷题知识点之方法覆盖(方法重写)和方法重载的区别

不多说&#xff0c;直接上干货&#xff01; https://www.nowcoder.com/ta/review-java/review?query&asctrue&order&page6 方法重写的原则&#xff1a; 重写方法的方法名称、参数列表必须与原方法的相同&#xff0c;返回类型可以相同也可以是原类型的子类型(从Jav…

7-26 Windows消息队列

7-26 Windows消息队列&#xff08;25 分&#xff09; 消息队列是Windows系统的基础。对于每个进程&#xff0c;系统维护一个消息队列。如果在进程中有特定事件发生&#xff0c;如点击鼠标、文字改变等&#xff0c;系统将把这个消息加到队列当中。同时&#xff0c;如果队列不是空…

java for循环遍历解释,三种for循环遍历

import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class For{public static void main(String[]args) {//new一个集合对象List alListnew ArrayList();alList.add(1);//自动转成IntegeralList.add("abc");alList.add(a);//一般f…