php 文件缓存
2016-01-07 11:02
609 查看
class fileCache{ private $cachePath; private $cacheFileName; private $cacheFilePath; private $expiresTime; private $data=""; public function fileCache($cacheTime=10,$cachePath="cache/"){ $this->expiresTime=$cacheTime; $this->cachePath=$cachePath; $this->createCacheName(); } public function setData($data){ $this->data=$data; } public function createCacheName(){ $url=$_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI']; $this->cacheFileName=md5($url); $this->cacheFilePath=$this->cachePath.$this->cacheFileName.".php"; } public function buildCache(){ ob_start(); echo $this->data; $content=ob_get_contents(); ob_flush(); ob_clean(); file_put_contents($this->cacheFilePath, $content); } public function readCache(){ $cacheFile=$this->cacheFilePath; if(file_exists($cacheFile)){ include $cacheFile; }else{ return false; } return true; } public function cacheTime(){ $cacheFile=$this->cacheFilePath; if(!file_exists($cacheFile)){ return true; }elseif(fileatime($cacheFile)+$this->expiresTime<=time()){ return true; } return false; } } ///// $cache=new fileCache(2, "cache/"); if($cache->cacheTime()){ $cache->setData("hello world !"); $cache->buildCache(); }else{ $cache->readCache(); }
相关文章推荐
- PHP中声明一个空数组
- PhpStorm中terminal窗口字体修改
- 石头学PHP(一):Windows下的PHP7环境搭建
- php写的可以在本机发送iOS push程序
- php中&符号什么意思
- PHP中ltrim与rtrim去除左右空格及特殊字符实例
- Php-5.3.29编译安装
- php正则表达式验证 preg_match
- 20.12. smtplib — SMTP protocol client(SMTP 协议 客户端)
- PHP中单引号和双引号的区别
- PHP_EOL、DIRECTORY_SEPARATOR的作用
- Yii 1.0数据库操作 查询、增加、更新、删除
- yii2中数据库查询
- PHP 判断是否包含在某个字符串中
- tftp+dhcp+vsftp
- Windows下php环境变量的配置
- ubuntu ftp server-转
- php实现word文档在线浏览功能,配置安装手记 (转载)
- php composer
- Awesome PHP