首页 / 缓存 / PHP一个小巧的缓存类_PHP教程
PHP一个小巧的缓存类_PHP教程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP一个小巧的缓存类_PHP教程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1770字,纯文字阅读大概需要3分钟。
内容图文
![PHP一个小巧的缓存类_PHP教程](/upload/InfoBanner/zyjiaocheng/176/c0c41ebdbe664d55aa0dfc401bfd7533.jpg)
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<filemtime($_SERVER[SCRIPT_FILENAME])){
return 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,缓存文件也会...
内容总结
以上是互联网集市为您收集整理的PHP一个小巧的缓存类_PHP教程全部内容,希望文章能够帮你解决PHP一个小巧的缓存类_PHP教程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。