1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 很实用的php的缓存类文件示例

很实用的php的缓存类文件示例

时间:2018-12-16 01:48:24

相关推荐

很实用的php的缓存类文件示例

后端开发|php教程

php,示例,文件

后端开发-php教程

这是PHP的缓存类文件:cache.php

安卓全选反选源码全,vscode老版本下载,ubuntu 没有lcd,tomcat 8内存设置,爬虫上班,php加密解密数字,宣城seo推广价格优惠,网站弹窗广告js下载,企业官网免费模板下载地址lzw

cacheLimitTime = $cacheLimitTime; $this->cacheFileName = $this->getCacheFileName(); ob_start();} /* * 检查缓存文件是否在设置更新时间之内 * 返回:如果在更新时间之内则返回文件内容,反之则返回失败 */function cacheCheck(){ if( file_exists( $this->cacheFileName ) ) { $cTime = $this->getFileCreateTime( $this->cacheFileName ); if( $cTime + $this->cacheLimitTime > time() ) { echo file_get_contents( $this->cacheFileName ); ob_end_flush(); exit; } } return false;}/* * 缓存文件或者输出静态 * string $staticFileName 静态文件名(含相对路径) */function caching( $staticFileName = "" ){ if( $this->cacheFileName ) { $cacheContent = ob_get_contents(); ob_end_flush(); if( $staticFileName ) { $this->saveFile( $staticFileName, $cacheContent ); } if( $this->cacheLimitTime ) $this->saveFile( $this->cacheFileName, $cacheContent ); }} /* * 清除缓存文件 * string $fileName 指定文件名(含函数)或者all(全部) * 返回:清除成功返回true,反之返回false */function clearCache( $fileName = "all" ) { if( $fileName != "all" ) { $fileName = $this->cacheRoot . strtoupper(md5($fileName)).".".$this->cacheFileExt; if( file_exists( $fileName ) ) { return @unlink( $fileName ); }else return false; } if ( is_dir( $this->cacheRoot ) ) { if ( $dir = @opendir( $this->cacheRoot ) ) { while ( $file = @readdir( $dir ) ) { $check = is_dir( $file ); if ( !$check ) @unlink( $this->cacheRoot . $file ); } @closedir( $dir ); return true; }else{ return false; } }else{ return false; }}/*根据当前动态文件生成缓存文件名*/function getCacheFileName() { return $this->cacheRoot . strtoupper(md5($_SERVER["REQUEST_URI"])).".".$this->cacheFileExt;}/* * 缓存文件建立时间 * string $fileName 缓存文件名(含相对路径) * 返回:文件生成时间秒数,文件不存在返回0 */function getFileCreateTime( $fileName ) { if( ! trim($fileName) ) return 0; if( file_exists( $fileName ) ) { return intval(filemtime( $fileName )); }else return 0;} /* * 保存文件 * string $fileName 文件名(含相对路径) * string $text 文件内容 * 返回:成功返回ture,失败返回false */function saveFile($fileName, $text) { if( ! $fileName || ! $text ) return false; if( $this->makeDir( dirname( $fileName ) ) ) { if( $fp = fopen( $fileName, "w" ) ) { if( @fwrite( $fp, $text ) ) { fclose($fp); return true; }else { fclose($fp); return false; } } } return false;}/* * 连续建目录 * string $dir 目录字符串 * int $mode 权限数字 * 返回:顺利创建或者全部已建返回true,其它方式返回false */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;}}?>

街拍论坛源码下载,ubuntu软件加密,c 调用python爬虫,qr.php,丹东网站seolzw

这是a.php文件

ucosii 源码,vscode程序代写,ubuntu20.10不卡了,tomcat启动没,sqlite调用语句,dz论坛插件破解版,前端技术框架vue学习,爬虫json解析Array,php 递归 数组,大港seo,php商城网站源代码,网页设计注册页面代码,淘宝免费模板源码下载,cms内容网站管理系统,狼人杀微信小程序模板lzw

以下就是调用类文件实例

include( "cache.php" );$cache = new cache(30);$cache->cacheCheck();echo date("Y-m-d H:i:s");$cache->caching();

输出结果为

-08-12 10:10:10(已成功缓存30秒)//即30秒内的结果都不变

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。