//文件缓存类

 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. codeforces 55 div2 C.Title 模拟

    C. Title time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  2. [原]visual studio 将(无扩展名)文件以某种(C++)方式阅读(映射)

    工具 选项 文本编辑器 文件扩展名 选择“将无扩展····”后面设置一下就可以

  3. poj3734矩阵快速幂

    挑战上面的题目,感觉脑洞很大 分别找红蓝个数全为偶,全为奇,一奇一偶的个数ai,bi,ci 转移矩阵是| 2 1 0 |,是一个对称矩阵(会不会有什么联系.) | 2 2 2 | | 0 1 2 | ...

  4. 在Firefox中发现一个在Linux下查看chm文档的插件

    在Firefox浏览器插件中搜索插件chmfox插件,安装后就可以在linux下通过Firefox浏览器阅读chm文档了.

  5. CF 920

    t1 随便乱搞 t2 随便乱搞 然后wa了三发,QAQ t3 随便乱搞 t4 邻接表+堆 对进出进行一个统计 然后时间到了...

  6. Django——ContentType(与多个表建立外键关系)及ContentType-signals的使用

    一.ContentType 在django中,有一个记录了项目中所有model元数据的表,就是ContentType,表中一条记录对应着一个存在的model,所以可以通过一个ContentType表的 ...

  7. keras_基本网络层结构(1)_常用层

    参考文献: https://blog.csdn.net/sinat_26917383/article/details/72857454 http://keras-cn.readthedocs.io/e ...

  8. WebGL编程指南案例解析之绘制一个点

    <!DOCTYPE html> <html> <head> <title>webgl</title> <style type=&quo ...

  9. 为Python编写一个简单的C语言扩展模块

    最近在看pytorh方面的东西,不得不承认现在这个东西比较火,有些小好奇,下载了代码发现其中计算部分基本都是C++写的,这真是要我对这个所谓Python语音编写的框架或者说是库感觉到一丢丢的小失落,细 ...

  10. batchsize的选择

    参考链接:http://www.zhihu.com/question/32673260 其中链接中提到,theano Python接口,支持RBM,DBN,MLP...等