//文件缓存类

 class FileCache {
private $cacheTime = 3600; //默认缓存时间 秒
private $cacheDir = './filecache'; //缓存绝对路径
private $md5 = true; //是否对键进行加密
private $suffix = ".php"; //设置文件后缀 public function __construct($config){
if( is_array( $config ) ){
foreach( $config as $key=>$val ){
$this->$key = $val;
}
}
} //设置缓存
public function set($key,$val,$leftTime=null){
$key = $this->md5 ? md5($key) : $key;
$leftTime = $leftTime ? $leftTime : $this->cacheTime;
!file_exists($this->cacheDir) && mkdir($this->cacheDir,0777);
$file = $this->cacheDir.'/'.$key.$this->suffix;
$val = serialize($val);
@file_put_contents($file,$val) or $this->error(__line__,"文件写入失败");
@chmod($file,0777) or $this->error(__line__,"设定文件权限失败");
@touch($file,time()+$leftTime) or $this->error(__line__,"更改文件时间失败");
} //得到缓存
public function get($key){
$this->clear();
if( $this->_isset($key) ){
$key_md5 = $this->md5 ? md5($key) : $key;
$file = $this->cacheDir.'/'.$key_md5.$this->suffix;
$val = file_get_contents($file);
return unserialize($val);
}
return null;
} //判断文件是否有效
public function _isset($key){
$key = $this->md5 ? md5($key) : $key;
$file = $this->cacheDir.'/'.$key.$this->suffix;
if( file_exists($file) ){
if( @filemtime($file) >= time() ){
return true;
}else{
@unlink($file);
return false;
}
}
return false;
} //删除文件
public function _unset($key){
if( $this->_isset($key) ){
$key_md5 = $this->md5 ? md5($key) : $key;
$file = $this->cacheDir.'/'.$key_md5.$this->suffix;
return @unlink($file);
}
return false;
} //清除过期缓存文件
public function clear(){
$files = scandir($this->cacheDir);
foreach ($files as $val){
if (@filemtime($this->cacheDir."/".$val) < time()){
@unlink($this->cacheDir."/".$val);
}
}
} //清除所有缓存文件
public function clear_all(){
$files = scandir($this->cacheDir);
foreach ($files as $val){
@unlink($this->cacheDir."/".$val);
}
} private function error($line,$msg){
die("出错文件:".__file__."/n出错行:$line/n错误信息:$msg");
} 调用方法:
    
           //cacheTime 缓存时间 1个小时
$cacheFile = new FileCache(array('cacheTime'=>3600,'suffix'=>'.php'));
$cache_1 =$cacheFile->get('key1');
$cache_2 =$cacheFile->get('key2');
if( $cache_1 ){
$resArr_result = $cache_1;
}else{
//读数据库$resArr_result = array(***);
//设置缓存
$cacheFile->set('key1',$resArr_result);
}

php 文件缓存类的更多相关文章

  1. PHP文件缓存类

    <?php /** * @desc 文件缓存 */ class Cache{ const C_FILE = '/Runtime/'; private $dir = ''; const EXT = ...

  2. 一个不错的PHP文件页面缓存类

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

  3. php 文件缓存

    http://www.oschina.net/code/snippet_162279_6098 <?php class cache {       private static $_instan ...

  4. php文件缓存

    1.最新代码 <?php class cache { private static $_instance = null; protected $_options = array( 'cache_ ...

  5. android文件缓存,并SD卡创建目录未能解决和bitmap内存溢出解决

    1.相关代码: 加入权限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ...

  6. C#之文件缓存

    写在开头 今天就放假了,照理说应该写今年的总结了,但是回头一看,很久没有写过技术类的文字了,还是先不吐槽了. 关于文件缓存 写了很多的代码,常常在写EXE(定时任务)或者写小站点(数据的使用和客户端调 ...

  7. php文件缓存方法总结

    为大家分享很全的php文件缓存,供大家参考,具体内容如下 <?php class cache {  private static $_instance = null;    protected ...

  8. php文件缓存数据

    最近在做微信的摇一摇跑马活动,实现原理是用户摇动手机,通过ajax往数据库写入数据(小马跑的步数),然后PC端用过ajax每一秒钟从数据库中调取一次数据(小马跑的步数),然后显示在PC屏幕上,这样就会 ...

  9. 很实用的php的缓存类文件示例

    http://www.php.cn/php-weizijiaocheng-376603.html <?php /* * 缓存类 cache */ class cache { //缓存目录 var ...

随机推荐

  1. BZOJ 3572 【HNOI2014】 世界树

    题目链接:世界树 首先看到\(\sum m_i\le 3\times 10^5\)这个条件,显然这道题就需要用虚树了. 在我们构建出虚树之后,就可以用两遍\(dfs\)来求出离每个点最近的议事处了.然 ...

  2. JVM知识总结-运行时区域划分

    区域简介 JVM运行时区域有些随着虚拟机进程的启动而存在,有些依赖于用户线程的启动和结束而建立和销毁,大致分为以下几类:方法区,虚拟机栈,本地方法栈,堆,程序计数器,概念图如下(源于<深入理解J ...

  3. mongodb 用户点赞功能理论实现[转载]

    在 posts(文章) 集合中储存对该文章点赞的用户的 _id 的数组,例如: // posts { _id: ObjectID('4e7020cb7cac81af7136236b'), users_ ...

  4. FastJson中文乱码

    初学springboot使用fastJson替换默认的jackson后出现中文乱码 解决方式1: import java.util.ArrayList; import java.util.List; ...

  5. Java队列的两种实现方式

    1. 基于数组 package Algorithm.learn; import java.util.Arrays; /** * Created by liujinhong on 2017/3/7. * ...

  6. MIPS 汇编指令学习

    MIPS 寄存器 MIPS comes with 32 general purpose registers named $0. . . $31Registers also have symbolic ...

  7. Sublime 中文标题乱码

    ---title:Sublime 中文标题乱码--- #markdown语法(非Github Flavored) #解决办法: 在用户设置里添加一项,强制不根据 dpi 缩放dpi_scale: 1. ...

  8. 解决IE8地址栏没有下拉

    装了IE8,但是地址栏没有下拉,下载下面的文件,把两个文件都复制到 系统盘windows/system32,然后cmd下分别执行 regsvr32 chsbrkr.dll regsvr32 chtbr ...

  9. Beta阶段第2周/共2周 Scrum立会报告+燃尽图 08

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2389] 版本控制:https://git.coding.net/liuyy08 ...

  10. 再谈Spring AOP

    1.AOP的基本概念 在进行AOP开发前,先熟悉几个概念: 连接点(Jointpoint):表示需要在程序中插入横切关注点的扩展点,连接点可能是类初始化.方法执行.方法调用.字段调用或处理异常等等,S ...