【PHP】缓存技术!缓存类,看看原理吧
2016-08-08 15:39
417 查看
<?php /** * 缓存 */ class Cache { private $cachedir = './cache/'; //目录 private $cachetime = 0; //更新时间 private $cachename = ""; //文件名称 private $cachefile = ".php"; //文件后缀名 function __construct($cachetime) { if(intval($cachetime)){ $this->cachetime =$cachetime; $this->cachename=$this->getFileName(); ob_start(); } } /** * [cacheCheck 检测缓存是否过期] * @return [type] [description] */ public function cacheCheck($name=''){ $name=$this->pathSet($name); if(file_exists($name)) { $ctime=$this->createCacheTime($name); if ($ctime+$this->cachetime>time()) { echo "【以下为缓存】"; echo file_get_contents($name); ob_end_flush(); exit(); } } elseif(file_exists($this->cachename)) { $ctime=$this->createCacheTime($this->cachename); if ($ctime+$this->cachetime>time()) { echo "【以下为缓存】"; echo file_get_contents($this->cachename); ob_end_flush(); exit(); } } return false; } /** * [pathSet 名称格式化] * @param [type] $name [description] * @return [type] [description] */ public function pathSet($name){ return "./".$name; } /** * [putCache 写出缓存] * @param string $name [description] * @return [type] [description] */ public function putCache($name=''){ if ($this->cachename) { $content = ob_get_contents(); //获取当前页面的所有内容 ob_end_flush(); if ($name) { $this->saveCache($name,$content); } elseif($this->cachetime){ $this->saveCache($this->cachename,$content); } } } /** * [saveCache 保存缓存] * @param [type] $name [description] * @param [type] $content [description] * @return [type] [description] */ public function saveCache($name,$content){ if (!$name || !$content) return false; if ($this->makeDir(dirname($name))) { if ($fp=fopen($name,"w")) { if (@fwrite($fp,$content)) { fclose($fp); return true; }else{ fclose($fp); return false; } } } return false; } /** * [clearCache 清除缓存] * @param string $fileName [description] * @return [type] [description] */ public function clearCache($fileName = "all") { if($fileName !="all"){ $fileName = $this->cachedir.strtoupper(md5($fileName)).$this->cachefile; if (file_exists($fileName)) { return @unlink($fileName); }else{ return false; } } if(is_dir($this->cachedir)){ if ($dir = @opendir($this->cachedir)) { while ($file = @readdir($dir)) { $check = is_dir($file); if (!$check) { @unlink($this->cachedir.$file); } } @closedir($dir); return true; }else{ return false; } }else{ return false; } } /** * [makeDir 创建文件] * @param [type] $dirname [description] * @return [type] [description] */ public function makeDir($dir,$mode='0777'){ if (!$dir){return 0;} $dir = str_replace("\\","/",$dir); $mdir = ""; foreach (explode("/",$dir) as $val) { $mdir .=$val."/"; if ($val == ".." || $val == "." || trim($val)== "")continue; if (!file_exists($mdir)) { if (!@mkdir($mdir,$mode)) { return false; } } } return true; } /** * [createCacheTime 创建缓存时间名] * @param [type] $cachename [description] * @return [type] [description] */ public function createCacheTime($cachename){ if(!trim($cachename))return 0; if(file_exists($cachename)){ return intval(filemtime($cachename)); }else{ return 0; } } /** * [getFileName 生成一个缓存文件名] * @return [type] [description] */ public function getFileName(){ return $this->cachedir.strtoupper(md5($_SERVER['REQUEST_URI'])).$this->cachefile; } }
index.php
<?php include $_SERVER['DOCUMENT_ROOT']."/slike/cache.php"; $cache=new Cache(30); //$cache->clearCache(); if(!$cache->cacheCheck('111.php')){ echo file_get_contents($_SERVER['DOCUMENT_ROOT']."/slike/view/index.html"); echo date("Y-m-d H:i:s"); $cache->putCache('111.php'); }
相关文章推荐
- php生成html静态页面缓存技术原理【附实例】
- php生成静态html页面缓存技术原理+实例
- php之memcache缓存技术
- PHP常用的缓存技术汇总
- PHP常用的缓存技术汇总
- 深入理解PHP高级技巧、面向对象与核心技术之调整浏览器缓存
- 分布式缓存技术redis学习系列(四)——redis高级应用(集群搭建、集群分区原理、集群操作)
- 缓存技术详谈—php
- PHP生成HTML的技术原理
- PHP网页缓存技术
- 深入理解PHP Opcode缓存原理
- PHP图像识别技术原理与实现
- CPU中的缓存技术原理
- 以Discuz!为例,分析PHP中的缓存技术
- 秋色园QBlog技术原理解析:性能优化篇:缓存总有失效时,持续的缓存方案(十四)
- PHP常用的缓存技术汇总
- 缓存技术详谈—php
- CPU中的缓存技术原理
- 深入理解PHP Opcode缓存原理
- 【缓存技术原理】浏览器端缓存机制详解