2011-03-09 103 views
0

有沒有一種方法來緩存drupal系統頁面(例如分類/術語/%,論壇/%,節點)授權用戶沒有核心黑客?緩存drupal系統頁面

回答

0

你可以在你的自定義模塊上啓動hook_menu_alter,然後從那裏你可以做任何你想要的路徑(分類/術語/%)。

檢查這些路徑的函數回調是什麼。例如:

mysql> 
select * from menu_router where path like '%taxonomy/term/%'; 

它說頁回調是taxonomy_term_page。你並不需要所有的代碼複製到您的自定義功能,所有你需要做的是這樣的:

function mymodule_menu_alter(&$items) { 
    // Route taxonomy/term/% to my custom caching function. 
    $items['taxonomy/term/%']['page callback'] = 'mymodule_cached_taxonomy_term_page'; 
} 

function mymodule_cached_taxonomy_term_page($term) { 
    // Retrieve from persistent cache. 
    $cache = cache_get('taxonomy_term_'. $term); 

    // If data hasn't expired from cache. 
    if(!empty($cache->data) && ($cache->created < $cache->expire)) { 
    return $cache->data; 
    } else { 
    // Else rebuild the cache. 
    $term_page = taxonomy_term_page($term); 
    cache_set('taxonomy_term_'. $term, $term_page, 'cache_page', strtotime('+30 minute')); 
    return $term_page; 
    } 
} 

如果走這條路,你就會想要與cache_getcache_set熟悉。你可能也想看看Lullabot的優秀緩存article

您可以按照相同的方法查找論壇/%,節點以及其他任何您想要的內容。快樂緩存!

+0

我想過這種方式。但是taxonomy_term_page不僅返回html頁面代碼,還會創建麪包屑並添加feed。 它不會在mymodule_cached_taxonomy_term_page中工作。其他回調函數additionaly可以使用drupal_set_title,drupal_add_js,drupal_add_css等。 – 2011-03-11 05:06:18

+0

我知道了,我還看到了taxonomy_term_page代碼=(。如果不像上面提到的那樣緩存taxonomy_term_page的結果,那麼如果深入瞭解該函數,並有選擇地將代碼複製到自定義函數中,代碼如何你需要,然後分別調用feeds功能嗎?你試過嗎?讓我們知道你發現了什麼。 – 2011-03-11 05:12:32

+0

是的,我用taxonomy_term_page類似的代碼。它工作正常。現在我正在尋找緩存其他核心頁面的解決方案(節點,node /%,forum)我不想把所有的drupal核心回調函數都移到我的模塊中。) – 2011-03-11 06:11:39