中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

一個(gè)不錯(cuò)的PHP緩存類

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用

緩存在實(shí)際使用當(dāng)中應(yīng)用很廣泛,可以減輕對(duì)服務(wù)器數(shù)據(jù)庫的訪問,提高運(yùn)行速度。目前很多CMS內(nèi)容管理系統(tǒng)中頻繁使用緩存機(jī)制來提高系統(tǒng)運(yùn)行的效率。下面是一個(gè)寫得不錯(cuò)的緩存類,可以參考下緩存的機(jī)制與寫法。

cache.php 代碼如下:

    <?    
    /*  
    用戶需要事先定義的常量:  
    _CachePath_        模板緩存路徑  
    _CacheEnable_        自動(dòng)緩存機(jī)制是否開啟,未定義或?yàn)榭眨硎娟P(guān)閉自動(dòng)緩存機(jī)制  
    _ReCacheTime_        自動(dòng)重新緩存間隔時(shí)間,單位為秒,未定義或?yàn)榭,表示關(guān)閉自動(dòng)重新緩存  
    */    
        
    class cache   
    {  
        var $cachefile;    
        var $cachefilevar;    
        
        function cache()   
        {    
            //生成當(dāng)前頁的Cache組文件名 $this->cachefilevar 及文件名 $this->cachefile    
            //動(dòng)態(tài)頁的參數(shù)不同對(duì)應(yīng)的Cache文件也不同,但是每一個(gè)動(dòng)態(tài)頁的所有Cache文件都有相同的文件名,只是擴(kuò)展名不同    
            $s=array(".","/");$r=array("_","");    
            $this->cachefilevar=str_replace($s,$r,$_SERVER["SCRIPT_NAME"])."_".$_GET[_ActionVar_];    
            $this->cachefile=$this->cachefilevar.".".md5($_SERVER["REQUEST_URI"]);    
        }    
        
        //刪除當(dāng)前頁/模塊的緩存    
        function delete()   
        {    
            //刪除當(dāng)前頁的緩存    
            $d = dir(_CachePath_);    
            $strlen=strlen($this->cachefilevar);    
            //返回當(dāng)前頁的所有Cache文件組    
            while (false !== ($entry = $d->read()))   
            {    
                if (substr($entry,0,$strlen)==$this->cachefilevar)   
                {    
                    if (!unlink(_CachePath_."/".$entry)) {echo "Cache目錄無法寫入";exit;}    
                }    
            }    
        }    
        
        //判斷是否已Cache過,以及是否需要Cache    
        function check()   
        {    
            //如果設(shè)置了緩存更新間隔時(shí)間 _ReCacheTime_    
            if (_ReCacheTime_+0>0)  
            {    
                //返回當(dāng)前頁Cache的最后更新時(shí)間    
                $var=@file(_CachePath_."/".$this->cachefilevar);$var=$var[0];    
                //如果更新時(shí)間超出更新間隔時(shí)間則刪除Cache文件    
                if (time()-$var>_ReCacheTime_)   
                {    
                    $this->delete();$ischage=true;    
                }    
            }    
            //返回當(dāng)前頁的Cache    
            $file=_CachePath_."/".$this->cachefile;    
            //判斷當(dāng)前頁Cache是否存在 且 Cache功能是否開啟    
            return (file_exists($file) and _CacheEnable_ and !$ischange);    
        }    
        
        //讀取Cache    
        function read()   
        {    
            //返回當(dāng)前頁的Cache    
            $file=_CachePath_."/".$this->cachefile;    
            //讀取Cache文件的內(nèi)容    
            if (_CacheEnable_) return readfile($file);    
            else return false;    
        }    
        
        //生成Cache    
        function write($output)   
        {    
            //返回當(dāng)前頁的Cache    
            $file=_CachePath_."/".$this->cachefile;    
            //如果Cache功能開啟    
            if (_CacheEnable_)   
            {    
                //把輸出的內(nèi)容寫入Cache文件    
                $fp=@fopen($file,'w');    
                if (!@fputs($fp,$output)) {echo "模板Cache寫入失敗";exit;}    
                @fclose($fp);    
                //如果設(shè)置了緩存更新間隔時(shí)間 _ReCacheTime_    
                if (_ReCacheTime_+0>0)   
                {    
                    //更新當(dāng)前頁Cache的最后更新時(shí)間    
                    $file=_CachePath_."/".$this->cachefilevar;    
                    $fp=@fopen($file,'w');    
                    if (!@fwrite($fp,time())) {echo "Cache目錄無法寫入";exit;}    
                    @fclose($fp);    
                }    
            }    
        }    
    }    
    ?>  

類的使用:
    <?php    
        define("_CachePath_","./cache/");    
        define("_CacheEnable_","1");    
        define("_ReCacheTime_","43200");    
        include('cache.php');    
        $cache=new cache();    
        if ($cache->check())   
        {    
            $template=$cache->read();    
        }  
        else   
        {    
            ob_start();    
            ob_implicit_flush(0);    
    ?>    
        頁面內(nèi)容。。。。    
    <?php    
            $template = ob_get_contents();    
            $cache->write($template);    
        }    
    ?>    

標(biāo)簽: 代碼 服務(wù)器 數(shù)據(jù)庫

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇: PHP解壓ZIP

下一篇:ASP.NET加密解密