PHP常用类 – 缓存类 cache

yumo6665个月前 (06-02)技术文章33

<?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语言这些常见标准文件该如何使用?很基础也很重要

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

利用C语言读取BMP文件

什么是bmp文件BMP是bitmap的缩写形式,bitmap顾名思义,就是位图也即Windows位图。它一般由4部分组成:文件头信息块、图像描述信息块、颜色表(在真彩色模式无颜色表)和图像数据区组成。...

C语言中的fwrite 与 write的区别

在C语言中,fwrite 和 write 都是用于向文件或设备写入数据的函数,但它们有显著的区别,主要体现在以下几个方面:1. 函数来源和层次fwrite:属于C标准库函数,定义在 <stdio...

「C语言笔记」什么是ANSI C标准?

我们在很多地方都有看到过K&R C、ANSI C、ISO C、C89、C99、C11等标准,具体有什么不同呢?什么是K&R C?1978年,丹尼斯o里奇(Dennis Ritchie)...

calloc和realloc的使用以及二级指针作为函数参数的输入和输出

1.calloc与realloc的使用void *malloc(size_t size)size -- 内存块的大小,以字节为单位该函数返回一个指针 ,指向已分配大小的内存。如果请求失败,则返回 NU...