很实用的php的缓存类文件示例
http://www.php.cn/php-weizijiaocheng-376603.html
<?php
/*
* 缓存类 cache
*/
class cache {
//缓存目录
var $cacheRoot = "./cache/";
//缓存更新时间秒数,0为不缓存
var $cacheLimitTime = 0;
//缓存文件名
var $cacheFileName = "";
//缓存扩展名
var $cacheFileExt = "php";
/*
* 构造函数
* int $cacheLimitTime 缓存更新时间
*/
function cache( $cacheLimitTime ) {
if( intval( $cacheLimitTime ) )
$this->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;
}
}
?>
这是a.php文件
以下就是调用类文件实例
include( "cache.php" );
$cache = new cache(30);
$cache->cacheCheck();
echo date("Y-m-d H:i:s");
$cache->caching();
输出结果为
2017-08-12 10:10:10(已成功缓存30秒)
//即30秒内的结果都不变
很实用的php的缓存类文件示例的更多相关文章
- (实用篇)PHP缓存类完整实例
本文完整描述了一个简洁实用的PHP缓存类,可用来检查缓存文件是否在设置更新时间之内.清除缓存文件.根据当前动态文件生成缓存文件名.连续创建目录.缓存文件输出静态等功能.对于采用PHP开发CMS系统来说 ...
- android NDK 实用学习(四)-类缓存
1,为什么需要类缓存: 答:由于频繁的查找类及类成员变量需要很大的时间与空间开销,可参考如下文章: http://www.ibm.com/developerworks/cn/java/j-jni/ h ...
- 一个不错的PHP文件页面缓存类
在php中缓存分类数据库缓存,文件缓存和内存缓存,下面我来给各位同学详细介绍PHP文件缓存类实现代码,有需要了解的朋友可参考. 页面缓存类 <?php /* * 缓存类 cac ...
- php 文件缓存类
//文件缓存类 class FileCache { private $cacheTime = 3600; //默认缓存时间 秒 private $cacheDir = './filecache'; / ...
- 分享个 之前写好的 android 文件流缓存类,专门处理 ArrayList、bean。
转载麻烦声明出处:http://www.cnblogs.com/linguanh/ 目录: 1,前序 2,作用 3,特点 4,代码 1,前序 在开发过程中,client 和 server 数据交流一 ...
- 一个很棒的PHP缓存类,收藏下
<?php class Cache { /** 缓存目录 **/ var $CacheDir = './cache'; /** 缓存的文件 **/ var $CacheFile = ''; /* ...
- ASP.NET Core 折腾笔记二:自己写个完整的Cache缓存类来支持.NET Core
背景: 1:.NET Core 已经没System.Web,也木有了HttpRuntime.Cache,因此,该空间下Cache也木有了. 2:.NET Core 有新的Memory Cache提供, ...
- PowerDesigner 使用教程(很具体,很实用)
原文地址为:PowerDesigner 使用教程(很具体,很实用) 1.PowerDesigner 使用教程 从今日开始,每天一部分内容,在每个工作日,争取让大家天天都有的看,每天内容不会太多. 有错 ...
- MyEclipse快捷键大全,很实用
Eclipse本身很快的,但是加上了myeclipse后,就狂占内存,而且速度狂慢,那如何让Eclipse拖着myeclipse狂飚呢?这里提供一个: 技巧:取消自动validation valid ...
随机推荐
- wildfly 10上使用最新的 Hibernate ORM OGM
ORM是关系型数据库连接:ogm是No sql数据库连接,Mongo, redis等. 1,下载ogm zip包,解压到wildfly_home\modules\system\layers\base, ...
- long long
1. ll a; scanf("%d",&a); 数据读入后,产生错误 2. const ll inf=1e18; 3. int * ll = ll ll * int = ...
- Jquery Mobile基本元素
移动端框架 安装: CDN: <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jq ...
- appium 切换native/ webview,findby,还有页面元素定位一直小于0的问题的解决
之前一直有个bug没有解决. 今天,终于解决了. 疑问过程: app是混合应用,项目做了H5优化之后,以前的用例执行总会失败,体现在原来的一个元素点击无反馈 排查原因:1.项目做了H5优化,2.测试的 ...
- 转:mysql分页原理和高效率的mysql分页查询语句
(转自:http://www.jb51.net/article/46015.htm) 以前我在mysql中分页都是用的 limit 100000,20这样的方式,我相信你也是吧,但是要提高效率,让分页 ...
- POJ 2774 Long Long Message (Hash + 二分)
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 34473 Accepted: 13 ...
- php mkdir No such file or director问题
有时使用mkdir创建目录时会出现 No such file or director这样的错误,导致这个错误的原是 比如你要创建目录 a\b\c目录,但是创建时父目录b也不存在时就会出现这样的问题. ...
- awk统计文件大小
在Linux系统中,经常会遇到某个目录下文件很多,要统计这些文件的空间大小.可以采用awk来实现.如下是实现这个功能的例子. vim sum.sh #!/bin/bash# sum.shcd //ba ...
- CodeForces149D dfs实现区间dp
http://codeforces.com/problemset/problem/149/D 题意 给一个合法的括号串,然后问这串括号有多少种涂色方案,当然啦!涂色是有限制的. 1,每个括号只有三种选 ...
- vue-获取dom节点
获取dom节点可以用ref属性,这个属性就是来获取dom对象的.看代码 这个属性就相当于给这个标签起了一个id样的东西 <input type="text" ref=&qu ...