2011-10-10 10 views
0

我將構建一個網站,其中包含數據庫中的動態內容(屬性如標題,URL等)。我想每次查詢所有內容並分配變量都是非常不現實的,所以我已經閱讀了關於緩存的內容。數據庫中的Smarty緩存站點屬性

我使用Smarty模板,系統。

include('libs/Smarty/Smarty.class.php'); 

    $smarty = new Smarty; 
    $smarty->setCaching(true); 

    if (!$smarty->isCached('index.tpl')) { 

    //query here and assign.. 
    $smarty->assign('title', 'Test'); 
    } 

    $smarty->display('themes/simple/index.tpl'); 
  1. 上空時,它再次檢查緩存中的代碼?如果我在數據庫網站屬性中進行了更改,我希望立即更改我的網站,可以將這些網站用於維護等非常重要的事情。
  2. 我是否真的需要查詢所有數據頁面加載來獲取網站信息,還是有任何解決方案,比如用緩存檢查數據庫行結構,或者類似的東西?

解決方案,我需要!


更新: 我嘗試clearCache的方法,但它似乎並沒有正常工作。我更新數據庫,但「clearCache」方法似乎沒有被觸發,或者什麼。

$smarty = new Smarty; 
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT); 

//just to test 
$smarty->clearCache('index.tpl'); 


if (!$smarty->isCached('index.tpl')) { 



//do stuff here 
+0

好吧,如果你想改變被立即提供給用戶,你不應該使用緩存可言,這是它:-) –

+0

有沒有辦法像檢查數據庫表已被更改,如果是的話,重新查詢一切,這應該節省一些時間?還是我誤會了? – John

+0

不是沒有查詢數據庫,這是你想要避免的。 –

回答

1

修改數據庫中的東西時,請通知Smarty清除相關緩存clearCache()。

您還可以根據存儲在數據庫中的某個時間戳檢查緩存的修改時間。

<?php 
$smarty = new Smarty(); 
$tpl = $smarty->createTemplate($template_file, $cache_id); 
$databaseLastModified = strtotime("yesterday"); // determine when the database was modified last 
if (!$tpl->isCached() || $tpl->cached->timestamp < $databaseLastModified) { 
    $tpl->force_cache = true; // in case of timestamp < modified 
    // load data for smarty to process 
    $tpl->assign('your', 'stuff'); 
} 
$tpl->display(); 
+0

我更新了我的帖子=)看看 – John

+0

好吧,如果你使用PhpMyAdmin或其他類似的工具對數據庫進行操作,那麼顯然你無法清除Smarty的緩存。你需要在你的管理工具中使用 - > clearCache()。 – rodneyrehm

+0

如果您更喜歡使用其他工具來處理數據庫,那麼您應該添加一列來添加上次更新時間(使用TRIGGER相當簡單)並嘗試上述代碼。 – rodneyrehm