首页 » 网站推广 » phpsetlimittime技巧_PHP常用类 – 缓存类 cache

phpsetlimittime技巧_PHP常用类 – 缓存类 cache

访客 2024-11-21 0

扫一扫用手机浏览

文章目录 [+]

/

缓存类 cache

phpsetlimittime技巧_PHP常用类 – 缓存类 cache

实 例:

phpsetlimittime技巧_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;

}

}

?>

相关文章

介绍直播新纪元,轻松进入直播的五大步骤

随着互联网技术的飞速发展,直播行业在我国逐渐崛起,越来越多的人选择通过直播这一新兴媒介展示自己、分享生活、传递价值。对于许多新手来...

网站推广 2025-01-03 阅读1 评论0

介绍相机美颜原理,科技与美学的完美结合

随着科技的发展,智能手机的摄像头功能日益强大,美颜相机成为了许多人拍照的首选。美颜相机不仅满足了人们对于美的追求,更在视觉上给人带...

网站推广 2025-01-03 阅读1 评论0

介绍磁铁的制造,科学与艺术的完美结合

磁铁,一种神秘的物质,自古以来就吸引了无数人的目光。它不仅具有独特的磁性,还能在工业、医疗、科研等领域发挥重要作用。磁铁是如何制造...

网站推广 2025-01-03 阅读1 评论0

介绍电瓶激活方法,让电池焕发新生

随着科技的不断发展,电动汽车逐渐成为人们出行的首选。而电瓶作为电动汽车的核心部件,其性能直接影响着车辆的续航里程和行驶体验。新购买...

网站推广 2025-01-03 阅读1 评论0