2012-10-11 27 views
1

我想構建電子商務平臺的緩存系統。如何打破ob_start()並在一些標籤後繼續

我選擇在頁面末尾使用ob_start('callback')ob_end_flush()

我將驗證是否有爲訪問的url創建的任何.cache文件,並且如果有文件,我將打印其內容。

我的問題是,我想保持購物車的生活,所以我不想緩存它。我怎樣才能做到這一點?

<?php 

    function my_cache_function($content) { 
     return $content; 
    } 

    ob_start('my_cache_function'); 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>test</title> 
</head> 
<body> 
    test 
    <?php 
     //some ob_break() ? 
    ?> 
    <div id="shopping-cart"> 
     this should be the content I do not want to cache it 
    </div> 
    <?php 
     // ob_continue() ? 
    ?> 

</body> 
</html> 
<?php 
    ob_end_flush(); 
?> 

預先感謝您!

回答

1

如果你這樣做,問題是內容將輸出之前的任何HTML放在之前。您可能需要的是將該內容保存在某個變量中,然後在緩存「模板」文件中使用佔位符,例如%SHOPPING-CART%

因此,您可以用str_replace替換爲真實的非緩存內容。

+0

即使用戶未登錄,購物車仍然可用。當用戶登錄時,將不存在緩存,因此我不想緩存的唯一項目是購物車,它將在每個頁面上可見。我相信你的解決方案能夠工作,所以謝謝你的回答。我不明白爲什麼我直到現在才意識到:)) –

1

你可以這樣說:

<?php 

    function my_cache_function($content) { 
     return $content; 
    } 
    $output = ""; 
    ob_start('my_cache_function'); 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>test</title> 
</head> 
<body> 
    test 
    <?php 
     $output .= ob_get_clean(); 
    ?> 
    <div id="shopping-cart"> 
     this should be the content I do not want to cache it 
    </div> 
    <?php 
     ob_start(); 
    ?> 

</body> 
</html> 
<?php 
     $output .= ob_get_clean(); 
     echo $output; 
?> 

即使並沒有真正意義。

1

我不確定Zulakis解決方案會一路走下去......這個改動怎麼樣?

<?php 
$pleaseCache=true; 
function my_cache_function($content) { 
    if($pleaseCache) 
    { 
     /// do your caching 
    } 
    return $content; 
} 
$output = ""; 
ob_start('my_cache_function'); 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>test</title> 
</head> 
<body> 
    test 
    <?php 
     $output .= ob_get_clean(); 
     $pleaseCache = false; 
     ob_start('my_cache_function'); 
    ?> 
    <div id="shopping-cart"> 
     this should be the content I do not want to cache it 
    </div> 
    <?php 
     $output .= ob_get_clean(); 
     $pleaseCache = true; 
     ob_start('my_cache_function'); 
    ?> 

</body> 
</html> 
<?php 
    $output .= ob_get_clean(); 
    ob_end_clean(); 
    echo $output; 
?> 

同樣,不知道這使得有很大的意義......但你有你的原因,我預設。

+0

謝謝你的回答..我給你和@Zulakis +1,因爲你已經嘗試了我所要求的。不幸的是,有人誤解了,我認爲blue112解決方案是最好的。即使我使用您的解決方案,購物車也將無法正常運行如果我已從緩存文件中獲取該文件, –

相關問題