2017-03-03 76 views
0

我的php代碼有問題。我用這個函數使用一個簡單的緩存系統。緩存後的PHP代碼[如何]

// Cache Function 
function cachestart($min){ 
    global $here; 
    global $cachefile; 
    $cachedosyasi = 'cache/' . 'file' . md5($here); 
    if(file_exists($cachefile) && (time() - $min*60 < filemtime($cachefile))){ 
    include($cachefile); 
    die; 
    }else{ 
     ob_start(); 
     global $cache; 
     $cache = 1; 
    } 
} 

function cachefinish(){ 
    global $cache; 
    global $cachefile; 
    if(@$cache){ 
     $ch = fopen($cachefile, 'w'); 
     fwrite($ch, ob_get_contents()); 
     fclose($ch); 
     ob_end_flush(); 
    } 
} 

我'嘗試是:

-- Some Queries (Controls, is user logged etc.) 
<< Start Caching >> (With cachestart()) 
-- Some Queries (Show entries in database.) 
<< Stop Caching >> (With cachefinish()) 
-- Some Queries (Comment box) 

但我不能這樣,因爲 「死」。我對此一無所知。

感謝您的幫助!

+0

所有查詢重新工作,我看到緩存之間的雙重一切:

function cachestart($min){ global $cachefile; if(file_exists($cachefile) && (time() - $min*60 < filemtime($cachefile))){ include($cachefile); //we are not caching return false; } else { ob_start(); //we are caching return true; } } 

在您的應用程序的啓動做出決定功能。 編輯:有人說「刪除'死'功能」,但評論刪除。 –

回答

1

所以這不是完美的,但使用您現有的代碼;剛剛返回truefalse,讓你的應用程序中進行基於這樣的決定:

if(!cachestart(60)) { 
    //page loaded from cache 
    //maybe do some queries for comment box 
    die(); //or call cachefinish() with a die() in there 
} 

//main application code