很实用的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 ...
随机推荐
- How To Install WildFly as a Service on Linux
Installing WildFly as a service on Linux has multiple advantages like automatic start on system boot ...
- HEOI2019退役总结
真的很快,一切就都已经尘埃落定了. 其实经历不是很圆满的时候,是不想写这一类游记总结的,但这次其实不太一样,总要让这段经历有始有终. 可能会很啰嗦…… 赛前 收到了若干鼓励,包括老师的手写祝福和学长学 ...
- 认识EasyUI——DataGrid的onClickRow事件
关键代码: $("#dg2").datagrid({ onClickRow: function (index, row) { //easyui封装好的时间(被单机行的索引,被单击行 ...
- Codeforces Gym 191033 E. Explosion Exploit (记忆化搜索+状压)
E. Explosion Exploit time limit per test 2.0 s memory limit per test 256 MB input standard input out ...
- POJ 3349 Snowflake Snow Snowflakes (Hash)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 48646 Accep ...
- parallels tools 安装
│ - kernel-devel-2.6.32-358.el6.x86_64 │ │ - dkms
- centos7 mysql-server 安装过程
官网下载安装mysql-server # wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm # rpm -i ...
- 如何修改const常量值
总结:这个跟计算机语言类别和编译器有关,本文是在linux环境下说明的. 分两种情况: 1. C语言: 2. C++语言: /*! * \Description: * \author scictor ...
- 【强大的Java集成开发工具】MyEclipse 2015 Stable 2.0 for Mac
[简介] MyEclipse是一款 Mac 上的Java 强大的集成开发工具,今天和大家分享最新的 MyEclipse 2015 Stable 2.0 版本,MyEclipse 2015 基于 Ecl ...
- str
print('字符串操作') s='abc DEF hij' print('首字母大写') print(s.capitalize()) print('全大写') print(s.upper()) pr ...