2011-03-09 125 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