PHP
发布时间:2022-04-09 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP文件缓存类实现代码,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP中缓存分类
和
内存缓存
,下面我来给各位同学详细介绍
PHP文件缓存类实现
代码,有需要了解的朋友可参
考。
代码如下:
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 = 3;
//缓存文件名
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;
}
/*
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;
}
/*
if( file_exists( $filename ) ) {
return intval(filemtime( $filename ));
}else return 0;
}
/*
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;
}
}
?>
大佬总结
以上是大佬教程为你收集整理的PHP文件缓存类实现代码全部内容,希望文章能够帮你解决PHP文件缓存类实现代码所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。