您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 连云港分类信息网,免费分类信息发布

PHP一个小巧的缓存类_PHP教程

2024/4/26 16:37:18发布48次查看
功能很简单,就是缓存整个页面,可以设定缓存时间,可以缓存特定的url,例如:test.php?id=12,当目标文件更新时,如test.php,缓存文件也会更新,即使仍处于缓存期内。
class cache
{
var $cache_dir = ./cache/;//this is the directory where the cache files will be stored;
var $cache_time = 120;//how much time will keep the cache files in seconds.
var $caching = false;
var $file = ;
function cache()
{
//constructor of the class
$this->file = $this->cache_dir . urlencode( $_server[request_uri] );
if(file_exists($this->file)) $expired = $this->check_expire();
else $expired = false;
if ( file_exists ( $this->file ) && ( filemtime ( $this->file ) + $this->cache_time ) > time() && !$expired )
{
//grab the cache:
$handle = fopen( $this->file , r);
do {
$data = fread($handle, 8192);
if (strlen($data) == 0) {
break;
}
echo $data;
} while (true);
fclose($handle);
exit();
}
else
{
//create cache :
$this->caching = true;
ob_start();
$now = time();
echo ;
}
}
function close()
{
//you should have this at the end of each page
if ( $this->caching )
{
//you were caching the contents so display them, and write the cache file
$data = ob_get_clean();
echo $data;
$fp = fopen( $this->file , w );
fwrite ( $fp , $data );
fclose ( $fp );
}
}
function check_expire(){
$fp = fopen($this->file,r);
preg_match(/:([d]+)-/,fread($fp,200),$time);
$modify_time = $time[1];
if($modify_timereturn true;
}
else{
return false;
}
}
}
用法:
//example :
$ch = new cache();
echo date(d m j g:i:s t y);
$ch->close();
http://www.bkjia.com/phpjc/486516.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/486516.htmltecharticle功能很简单,就是缓存整个页面,可以设定缓存时间,可以缓存特定的url,例如:test.php?id=12,当目标文件更新时,如test.php,缓存文件也会...
连云港分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录