Caching static page classes

WBOY
Release: 2016-07-29 08:49:47
Original
1171 people have browsed it
/**
 * 缓存静态页面类
 */
class HtmlCache{
 
    /**
     * 开启缓存
     * $filename缓存完整路径
     * $time缓存时间单位是秒,默认7200秒
     */
    public function cache_start($filename,$time=7200)
    {
        $cachefile = $filename;
        $cachetime = $time;
        ob_start();
        if(file_exists($cachefile) && (time()-$cachetime < filemtime($cachefile)))//判断文件是否存在或者是否在缓存时间内
        {
            include($cachefile);
            ob_end_flush();
            exit;
        }  
    }
    /**
     * 结束缓存
     * $filename缓存完整路径
     */
    public function cache_end($filename){
        $cachefile = $filename;
        $fp = fopen($cachefile, &#39;w&#39;);
        fwrite($fp, ob_get_contents());
        fclose($fp);
        ob_end_flush();  
    }
}
用法:
$HtmlCache = new HtmlCache();
$filename = "./cache.html";
$HtmlCache->cache_start($filename);
    //以下是输出的内容,放在cache_start和cache_end两个方法之间
    echo "cache";
$HtmlCache->cache_end($filename);
Copy after login

The above introduces the caching static page class, including caching content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!