PHP常用类 – 缓存类 cache

<?php

/*

* 缓存类 cache

* 实 例:

include( "cache.php" );

$cache = new cache(30);

$cache->cacheCheck();

echo date("Y-m-d H:i:s");

$cache->caching();

*/

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();

//echo $cacheContent;

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;

}

}

?>

相关文章

C语言错误处理不当详解

在C语言编程中,错误处理是一个至关重要的方面,但常常被忽视或处理不当。忽略函数返回值、不检查错误代码或未能从错误中优雅恢复,都可能导致程序行为不可预测、数据损坏、安全漏洞甚至程序崩溃。什么是错误处理不...

踩坑了!嵌入式C语言常见的几个陷阱!你遇到过吗?

要尊重编程语言的语法,要不然会出现一些意想不到的问题,导致bug。下面看几种情况。1. 运算符优先级C语言中有许多运算符,例如加减乘除、逻辑运算符等等。在表达式中,不同运算符的优先级不同,如果没有注意...

C语言控制标准I/O的5个函数

与底层I/O相比,标准I/O包除了可移植以外还有两个好处。第一,标准I/O有许多专门的函数简化了处理不同I/O的问题。例如,printf()把不同形式的数据转换成与终端相适应的字符串输出。第二,输入和...

C语言之文件操作

文件操作是C语言中非常重要的功能,用于读取和写入文件中的数据。C语言提供了一组标准库函数(如 fopen、fclose、fread、fwrite 等)来实现文件操作。以下是针对C语言初学者的详细讲解。...

35岁非科班出身程序员写下C语言文件读写操作(详解),牛

数据流和缓冲区是什么?文件类型和文件存取方式都有啥?数据流就C程序而言,从程序移进,移出字节,这种字节流就叫做流。程序与数据的交互是以流的形式进行的。进行C语言文件的读写时,都会先进行“打开文件”操作...

C语言这些常见标准文件该如何使用?很基础也很重要

谈到文件,先了解下什么是文本文件和二进制文件的区别吧!1、文本文件:存储时是将字符的ASCII值存在磁盘中,取的时候将数值(ASCII)翻译成对应的字符;2、二进制文件:存取的都是二进制;文件流指针:...