手记

php页面缓存的使用方法

<?php

/**ob_start();

ob_get_contents();

ob_end_flush();

**

**php页面缓存的使用方法,php页面缓存的小例子,如果数据库查询量较大,可以用cache来解决

**/

    function cache_start($dir,$expiretime){

        $filename = $dir.'\\'.sha1($_SERVER['REQUEST_URI']).'.html';

        ob_start();

        

        if(file_exists($filename) && (time()- filemtime($filename)<$expiretime)){

            include($filename);

            ob_end_flush();

            exit();

        }

        mkdir($dir);

    }

    function cache_end($dir){

        $filename = $dir.'\\'.sha1($_SERVER['REQUEST_URI']).'.html';

        echo $filename;

        

        $fp = fopen($filename,'w');

        fwrite($fp,ob_get_contents());

        fclose($fp);

        

        ob_end_flush();

    

    }

$dir = "D:\\\\php";

cache_start($dir, 10);

cache_end($dir);

0人推荐
随时随地看视频
慕课网APP