php文件缓存类用法实例分析


Posted in PHP onApril 22, 2015

本文实例讲述了php文件缓存类用法。分享给大家供大家参考。具体如下:

<?php
/**
 * 简单的文件缓存类
 *
 */
class XZCache{
 // default cache time one hour
 var $cache_time = 3600;
 // default cache dir
 var $cache_dir = './cache';
 public function __construct($cache_dir=null, $cache_time=null){
  $this->cache_dir = isset($cache_dir) ? $cache_dir : $this->cache_dir;
  $this->cache_time = isset($cache_time) ? $cache_time : $this->cache_time;
 }
 public function saveCache ($key, $value){
  if (is_dir($this->cache_dir)){
   $cache_file = $this->cache_dir . '/xzcache_' . md5($key);
   $timedif = @(time() - filemtime($cache_file));
   if ($timedif >= $this->cache_time) {
    // cached file is too old, create new
    $serialized = serialize($value);
    if ($f = @fopen($cache_file, 'w')) {
     fwrite ($f, $serialized, strlen($serialized));
     fclose($f);
    }
   }
   $result = 1;
  }else{
   echo "Error:dir is not exist.";
   $result = 0;
  }
  return $result;
 }
 /**
  * @return array 
  *   0 no cache
  *    1 cached
  *    2 overdue
  */
 public function getCache ($key) {
  $cache_file = $this->cache_dir . '/xzcache_' . md5($key);
  if (is_dir($this->cache_dir) && is_file($cache_file)) {
   $timedif = @(time() - filemtime($cache_file));
   if ($timedif >= $this->cache_time) {
    $result['cached'] = 2;
   }else{
    // cached file is fresh enough, return cached array
    $result['value'] = unserialize(file_get_contents($cache_file));
    $result['cached'] = 1;
   }
  }else {
   echo "Error:no cache";
   $result['cached'] = 0;
  }
  return $result;
 }
} //end of class

用法示例如下:

$cache = new XZCache();
$key = 'global';
$value = $GLOBALS;
$cache->saveCache($key, $value);
$result = $cache->getCache($key);
var_dump($result);

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
PHP与已存在的Java应用程序集成
Oct 09 PHP
PHP安装攻略:常见问题解答(三)
Oct 09 PHP
php adodb介绍
Mar 19 PHP
完美解决PHP中文乱码
Nov 26 PHP
360通用php防护代码(使用操作详解)
Jun 18 PHP
解析CodeIgniter自定义配置文件
Jun 18 PHP
php file_get_contents抓取Gzip网页乱码的三种解决方法
Nov 12 PHP
PH P5.2至5.5、5.6的新增功能详解
Jul 14 PHP
PHP编程中的常见漏洞和代码实例
Aug 06 PHP
php实现的单一入口应用程序实例分析
Sep 23 PHP
php文件上传你必须知道的几点
Oct 20 PHP
ecshop适应在PHP7的修改方法解决报错的实现
Nov 01 PHP
php实现将wav文件转换成图像文件并在页面中显示的方法
Apr 21 #PHP
PHP判断是否为空的几个函数对比
Apr 21 #PHP
php两种无限分类方法实例
Apr 21 #PHP
PHP中使用register_shutdown_function函数截获fatal error示例
Apr 21 #PHP
php的crc32函数使用时需要注意的问题(不然就是坑)
Apr 21 #PHP
wordpress安装过程中遇到中文乱码的处理方法
Apr 21 #PHP
PHP使用递归生成文章树
Apr 21 #PHP
You might like
别人整理的服务器变量:$_SERVER
2006/10/20 PHP
php遍历目录viewDir函数
2009/12/15 PHP
解析PHP 5.5 新特性
2013/07/02 PHP
PHP实现批量上传单个文件
2015/12/29 PHP
Laravel5.1框架注册中间件的三种场景详解
2019/07/09 PHP
JS实现仿新浪黄色经典滑动门效果代码
2015/09/27 Javascript
第一次接触JS require.js模块化工具
2016/04/17 Javascript
vue分类筛选filter方法简单实例
2017/03/30 Javascript
vue中实现移动端的scroll滚动方法
2018/03/03 Javascript
浅谈VUE-CLI脚手架热更新太慢的原因和解决方法
2018/09/28 Javascript
微信小程序解除10个请求并发限制
2018/12/18 Javascript
JSON的parse()方法介绍
2019/01/31 Javascript
NodeJs之word文件生成与解析的实现代码
2019/04/01 NodeJs
Javascript实现秒表计时游戏
2020/05/27 Javascript
Javascript数组及类数组相关原理详解
2020/10/29 Javascript
Node.js文本文件BOM头的去除方法
2020/11/22 Javascript
python网络编程学习笔记(九):数据库客户端 DB-API
2014/06/09 Python
Python装饰器用法实例总结
2018/02/07 Python
Python3之手动创建迭代器的实例代码
2019/05/22 Python
Pandas中DataFrame的分组/分割/合并的实现
2019/07/16 Python
Python Django基础二之URL路由系统
2019/07/18 Python
python开发实例之python使用Websocket库开发简单聊天工具实例详解(python+Websocket+JS)
2020/03/18 Python
html5 offlline 缓存使用示例
2013/06/24 HTML / CSS
HTML5基于flash实现播放RTMP协议视频的示例代码
2020/12/04 HTML / CSS
澳大利亚香水在线:Price Rite Mart
2017/12/28 全球购物
亚马逊新加坡官方网站:Amazon.sg
2020/03/25 全球购物
常见的软件开发流程有哪些
2015/11/14 面试题
业务副厂长岗位职责
2014/01/03 职场文书
文字自荐书范文
2014/02/10 职场文书
分层教学实施方案
2014/03/19 职场文书
邓小平理论心得体会
2014/09/09 职场文书
2014年人事科工作总结
2014/11/19 职场文书
毕业论文致谢格式模板
2015/05/14 职场文书
《海上日出》教学反思
2016/02/23 职场文书
初中政治教学反思
2016/02/23 职场文书
Oracle查看表空间使用率以及爆满解决方案详解
2022/07/23 Oracle