【发布时间】:2011-03-11 07:41:14
【问题描述】:
有没有办法为 授权用户 缓存 drupal 系统页面(例如分类/术语/%、论坛/%、节点)而无需核心破解?
【问题讨论】:
有没有办法为 授权用户 缓存 drupal 系统页面(例如分类/术语/%、论坛/%、节点)而无需核心破解?
【问题讨论】:
对于drupal 6,有http://drupal.org/project/authcache。我不相信 drupal 7 的模块已经准备好了。
【讨论】:
您可以在您的自定义模块上启动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_get 和cache_set。您可能还想查看 Lullabot 的出色缓存 article。
您可以对 forum/%、node 以及您想要的所有其他内容采用相同的方法。快乐的缓存!
【讨论】: