很实用的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 ...
随机推荐
- java 判断元素是否在数组内
一,先转为List,再使用contains()方法 String[] strArr = new String[] { "a", "b", "c&quo ...
- ArcGIS for qml -添加自由文本
源码:https://github.com/sueRimn/ArcGIS-for-qml-demos 实现地图上鼠标点击后添加自由文本功能 作者: 狐狸家的鱼 Github: 八至 版权声明:如需转载 ...
- A1094. The Largest Generation
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...
- 收集JavaScript中常用的方法函数
本文中,收集了一些比较常用的Javascript函数,希望对学习JS的朋友们有所帮助. 1. 字符串长度截取 function cutstr(str, len) { var temp, icount ...
- 认识Jmeter工具
1.Apache jmeter 是一个100%的纯java桌面应用,是Apache组织开发的基于java的压力测试工具.它最初被设计用于Web应用测试但后来扩展到其他测试领域,可以用于对静态的和动态的 ...
- 第十六节、特征描述符BRIEF(附源码)
我们已经知道SIFT算法采用128维的特征描述子,由于描述子用的是浮点数,所以它将会占用512字节的空间.类似的SUFR算法,一般采用64维的描述子,它将占用256字节的空间.如果一幅图像中有1000 ...
- Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public xxxxxxxx.
最近在使用 springBoot开发的时候, 使用PostMan访问接口, 返回一个 404 , 后台报一个 warn : Failed to read HTTP message: org.spr ...
- 《Kafka权威指南》读书笔记-操作系统调优篇
<Kafka权威指南>读书笔记-操作系统调优篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 大部分Linux发行版默认的内核调优参数配置已经能够满足大多数应用程序的运 ...
- java io系列24之 BufferedWriter(字符缓冲输出流)
转载请注明出处:http://www.cnblogs.com/skywang12345/p/io_24.html 更多内容请参考:java io系列01之 "目录" Buffere ...
- CentOS7 图形化方式安装 Oracle 18c 单实例
下载 Oracle 数据库,zip 包 https://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.h ...