php文件缓存类用法实例分析,_PHP教程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php文件缓存类用法实例分析,_PHP教程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1855字,纯文字阅读大概需要3分钟。
内容图文
![php文件缓存类用法实例分析,_PHP教程](/upload/InfoBanner/zyjiaocheng/187/18bfa5acabed4cf0a173d0165915cc42.jpg)
php文件缓存类用法实例分析,
本文实例讲述了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程序设计有所帮助。
http://www.bkjia.com/PHPjc/987891.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/987891.htmlTechArticlephp文件缓存类用法实例分析, 本文实例讲述了php文件缓存类用法。分享给大家供大家参考。具体如下: php/** * 简单的文件缓存类 * */class...
内容总结
以上是互联网集市为您收集整理的php文件缓存类用法实例分析,_PHP教程全部内容,希望文章能够帮你解决php文件缓存类用法实例分析,_PHP教程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。