PHP一个小巧的缓存类_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_time
}
else{
return false;
}
}
}
用法:
//Example :
$ch = new cache();
echo date("D M j G:i:s T Y");
$ch->close();
上一篇: 如何解决PHP无法修改header信息问题_PHP教程
下一篇: phpmyadmin常见错误
推荐阅读
-
一个PHP的远程图片抓取函数分享_PHP教程
-
asp.net 组合模式的一个例子_PHP教程
-
PHP内存缓存技术memcached 的安装和工作原理介绍_PHP教程
-
PHP的一个完美GIF等比缩放类,附带去除缩放黑背景_php实例
-
php array_merge函数使用需要注意的一个问题,phparray_merge_PHP教程
-
PHP跳转函数跟一个通用的操作提示类的编写
-
已知一个php的文件,怎么知道这个文件里面是否含有类,类名是什么
-
php的zip解压缩类pclzip使用示例_PHP教程
-
[视频教程]PHP100视频教程45:如何用PHP开发一个完整的网站
-
PHP中使用mktime获取时间戳的一个黑色幽默分析_PHP教程