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与ASP
Oct 09 PHP
搜索和替换文件或目录的一个好类--很实用
Oct 09 PHP
可定制的PHP缩略图生成程式(需要GD库支持)
Mar 06 PHP
Cannot modify header information错误解决方法
Oct 08 PHP
ThinkPHP分页类使用详解
Mar 05 PHP
php之Smarty模板使用方法示例详解
Jul 08 PHP
php中用memcached实现页面防刷新功能
Aug 19 PHP
Linux环境下php实现给网站截图的方法
May 03 PHP
php实现socket推送技术的示例
Dec 20 PHP
PHP htmlspecialchars_decode()函数用法讲解
Mar 01 PHP
Laravel5.1 框架数据库操作DB运行原生SQL的方法分析
Jan 07 PHP
Thinkphp 框架扩展之应用模式实现方法分析
Apr 27 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
php进程间通讯实例分析
2016/07/11 PHP
php 解决substr()截取中文字符乱码问题
2016/07/18 PHP
PHP基于ICU扩展intl快速实现汉字转拼音及按拼音首字母分组排序的方法
2017/05/03 PHP
Laravel构建即时应用的一种实现方法详解
2017/08/31 PHP
PHP自动载入类文件函数__autoload的使用方法
2019/03/25 PHP
php探针使用原理和技巧讲解
2019/09/17 PHP
laravel validate 设置为中文的例子(验证提示为中文)
2019/09/29 PHP
基于Jquery制作的幻灯片图集效果打包下载
2011/02/12 Javascript
JavaScript中变量提升 Hoisting
2012/07/03 Javascript
document.write()及其输出内容的样式、位置控制
2013/08/12 Javascript
Javascript图片上传前的本地预览实例
2014/06/16 Javascript
js获取指定时间的前几秒
2017/04/05 Javascript
SVG动画vivus.js库使用小结(实例代码)
2017/09/14 Javascript
利用HBuilder打包前端开发webapp为apk的方法
2017/11/13 Javascript
vue中选项卡点击切换且能滑动切换功能的实现代码
2018/11/25 Javascript
详解JavaScript中的函数、对象
2019/04/01 Javascript
angular6开发steps步骤条组件
2019/07/04 Javascript
vue使用showdown并实现代码区域高亮的示例代码
2019/10/17 Javascript
vue单应用在ios系统中实现微信分享功能操作
2020/09/07 Javascript
linux环境下安装pyramid和新建项目的步骤
2013/11/27 Python
几个提升Python运行效率的方法之间的对比
2015/04/03 Python
python中时间、日期、时间戳的转换的实现方法
2019/07/06 Python
Mac在python3环境下安装virtualwrapper遇到的问题及解决方法
2019/07/09 Python
Python 200行代码实现一个滑动验证码过程详解
2019/07/11 Python
python实现从wind导入数据
2019/12/03 Python
美国著名童装品牌:OshKosh B’gosh
2016/08/05 全球购物
英国领先的体验日提供商:Buyagift
2019/04/19 全球购物
Monki官网:斯堪的纳维亚的独立时尚品牌
2020/11/09 全球购物
专业毕业生个性的自我评价
2013/10/03 职场文书
骨干教师培训制度
2014/01/13 职场文书
《再别康桥》教学反思
2014/02/12 职场文书
离婚案件被告代理词
2015/05/23 职场文书
请客吃饭开场白
2015/06/01 职场文书
社会心理学学习心得体会
2016/01/22 职场文书
基于MySql验证的vsftpd虚拟用户
2021/11/07 MySQL
Apache Linkis 中间件架构及快速安装步骤
2022/03/16 Servers