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 相关文章推荐
APMServ使用说明
Oct 23 PHP
Php Cookie的一个使用注意点
Nov 08 PHP
php设计模式 Singleton(单例模式)
Jun 26 PHP
PHP在线生成二维码代码(google api)
Jun 03 PHP
PHP 解决session死锁的方法
Jun 20 PHP
浅析php变量修饰符static的使用
Jun 28 PHP
PHP编程实现微信企业向用户付款的方法示例
Jul 26 PHP
利用laravel+ajax实现文件上传功能方法示例
Aug 13 PHP
php 多进程编程父进程的阻塞与非阻塞实例分析
Feb 22 PHP
php设计模式之模板模式实例分析【星际争霸游戏案例】
Mar 24 PHP
实例化php类时传参的方法分析
Jun 05 PHP
PHP获取学生成绩的方法
Nov 17 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使用cookie显示用户上次访问网站日期的方法
2015/01/26 PHP
数据结构之利用PHP实现二分搜索树
2020/10/25 PHP
HR vs ForZe BO3 第一场 2.13
2021/03/10 DOTA
Jquery 扩展方法
2010/05/06 Javascript
jquery $.ajax()取xml数据的小问题解决方法
2010/11/20 Javascript
页面只能打开一次Cooike如何实现
2012/12/04 Javascript
jquery实现类似淘宝星星评分功能有截图
2014/09/15 Javascript
JS实现很酷的水波文字特效实例
2015/02/26 Javascript
jquery选择器中的空格与大于号&gt;、加号+与波浪号~的区别介绍
2016/06/24 Javascript
JS提示:Uncaught SyntaxError: Unexpected token ILLEGAL错误的解决方法
2016/08/19 Javascript
ES6概念 Symbol.keyFor()方法
2016/12/25 Javascript
jQuery 实现图片的依次加载图片功能
2017/07/06 jQuery
JavaScript作用域、闭包、对象与原型链概念及用法实例总结
2018/08/20 Javascript
angularjs自定义过滤器demo示例
2019/08/24 Javascript
javascript设计模式 ? 工厂模式原理与应用实例分析
2020/04/09 Javascript
vue mvvm数据响应实现
2020/11/11 Javascript
Python的迭代器和生成器
2015/07/29 Python
约瑟夫问题的Python和C++求解方法
2015/08/20 Python
pandas表连接 索引上的合并方法
2018/06/08 Python
Python实现的批量修改文件后缀名操作示例
2018/12/07 Python
python实现登录密码重置简易操作代码
2019/08/14 Python
python编写猜数字小游戏
2019/10/06 Python
jupyter notebook 添加kernel permission denied的操作
2020/04/21 Python
python输出数学符号实例
2020/05/11 Python
python3 中时间戳、时间、日期的转换和加减操作
2020/07/14 Python
Topshop美国官网:英国快速时尚品牌
2019/05/16 全球购物
省级四好少年事迹材料
2014/01/25 职场文书
精通CAD能手自荐书
2014/01/31 职场文书
安康杯竞赛活动总结
2014/05/05 职场文书
大专学生求职自荐信
2014/07/06 职场文书
机关作风建设剖析材料
2014/10/11 职场文书
党的群众路线教育实践活动整改方案
2014/10/28 职场文书
使用pycharm运行flask应用程序的详细教程
2021/06/07 Python
Python爬虫入门案例之爬取二手房源数据
2021/10/16 Python
Redis中有序集合的内部实现方式的详细介绍
2022/03/16 Redis
插件导致ECharts被全量引入的坑示例解析
2022/09/23 Javascript