2009-07-08 109 views
4

我使用Smarty的與我的PHP代碼,我想緩存一些網站的頁面,所以我用下面的代碼:在php中緩存文件的最佳方式是什麼?

// TOP of script 
ob_start(); // start the output buffer 
$cachefile ="cache/cachefile.html"; 
// normal PHP script 
$smarty->display('somefile.tpl.html') ; 
$fp = fopen($cachefile, 'w'); // open the cache file for writing 
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file 
fclose($fp); // close the file 
ob_end_flush(); // Send the output to the browser 

,但是當我在PHP文件的末尾打印ob_get_contents(),它是空的!實際上創建的緩存文件也是空的!所以我怎麼能在PHP中緩存文件時,我使用smarty我知道我可以使用smarty緩存,但它由於某種原因不適合我。

另外請給我關於APC緩存的信息。如何使用它?是值得使用在我的情況下,我瘦它只是爲了緩存數據庫查詢,我閱讀了PHP手冊,但我不能得到任何東西:) 坦克。

回答

1

我從Smarty的緩存更完整的示例文件(位於here)搗碎了一些代碼。另外,我不確定你在你的例子中使用了什麼,但是你應該使用smarty的方法來操作緩存。

require('Smarty.class.php'); 
    $smarty = new Smarty; 

    // 1 Means use the cache time defined in this file, 
    // 2 means use cache time defined in the cache itself 
    $smarty->caching = 2; 

    // set the cache_lifetime for index.tpl to 5 minutes 
    $smarty->cache_lifetime = 300; 

    // Check if a cache exists for this file, if one doesn't exist assign variables etc 
    if(!$smarty->is_cached('index.tpl')) { 
     $contents = get_database_contents(); 
     $smarty->assign($contents); 
    } 

    // Display the output of index.tpl, will be from cache if one exists 
    $smarty->display('index.tpl'); 

    // set the cache_lifetime for home.tpl to 1 hour 
    $smarty->cache_lifetime = 3600; 

    // Check if a cache exists for this file, if one doesn't exist assign variables etc 
    if(!$smarty->is_cached('home.tpl')) { 
     $contents = get_database_contents(); 
     $smarty->assign($contents); 
    } 

    // Display the output of index.tpl, will be from cache if one exists 
    $smarty->display('home.tpl'); 

至於APC緩存,它的工作方式與smarty相同。它們都將數據存儲在文件中一段特定的時間。每次你想訪問數據時,它都會檢查緩存是否有效,如果是,則返回緩存值。

但是,如果不使用Smarty的你可以使用APC這樣:
這個例子經過存儲在緩存中的數據庫查詢的結果,同樣,你可以修改這個整個頁面的輸出,而不是存儲,所以你不要不必經常運行昂貴的PHP函數。

// A class to make APC management easier 
class CacheManager 
{ 
    public function get($key) 
    { 
      return apc_fetch($key); 
    } 

    public function store($key, $data, $ttl) 
    { 
      return apc_store($key, $data, $ttl); 
    } 

    public function delete($key) 
    { 
      return apc_delete($key); 
    } 
} 

一些邏輯相結合,

function getNews() 
{ 
    $query_string = 'SELECT * FROM news ORDER BY date_created DESC limit 5'; 

    // see if this is cached first... 
    if($data = CacheManager::get(md5($query_string))) 
    { 
      // It was stored, return the value 
      $result = $data; 
    } 
    else 
    { 
      // It wasn't stored, so run the query 
      $result = mysql_query($query_string, $link); 
      $resultsArray = array(); 

      while($line = mysql_fetch_object($result)) 
      { 
       $resultsArray[] = $line; 
      } 

      // Save the result inside the cache for 3600 seconds 
      CacheManager::set(md5($query_string), $resultsArray, 3600); 
    } 

    // Continue on with more functions if necessary 
} 

此示例稍微從here修改。

+0

@lan艾略特是的,Smarty緩存是個好主意,但我不能使用它。因爲我只有一個$ smarty-> display('index.tpl');和其他像news.tpl其他頁面進入我的index.tpl中心像這樣{include file = $ page_center}然後在news.php文件中我使用這行$ smarty-> assign('page_center','news.tpl 「);但是當我啓用緩存它仍然顯示頁面中心的默認內容不news.tpl,但是當我的緩存它工作正常。 – mehdi 2009-07-11 14:00:17

1

你的意思是你調用ob_get_contents()你叫ob_end_flush()函數後再次?如果是這樣,你寫入文件的內容將被從PHP內存中「刪除」。

如果您想還是輸出HTML,ob_end_flush保存到一個變量,然後再傳遞到使用fwrite。您可以稍後使用該變量。

相關問題