You are here

Drupal专业开发指南 第15章 使用hook_flush_caches()

g089h515r806 的头像
Submitted by g089h515r806 on 星期六, 2009-08-22 15:12

 

Drupal有一个中心函数,用来清空所有的缓存,包括JavaScript和CSS缓存。这就是来自于includes/common.inc的drupal_flush_all_caches()函数:
 
/**
 * Flush all cached data on the site.
 *
 * Empties cache tables, rebuilds the menu cache and theme registries, and
 * exposes a hook for other modules to clear their own cache data as well.
 */
function drupal_flush_all_caches() {
    // Change query-strings on css/js files to enforce reload for all users.
    _drupal_flush_css_js();
    drupal_clear_css_cache();
    drupal_clear_js_cache();
    system_theme_data();
    drupal_rebuild_theme_registry();
    menu_rebuild();
    node_types_rebuild();
    // Don't clear cache_form - in-progress form submissions may break.
    // Ordered so clearing the page cache will always be the last action.
    $core = array('cache', 'cache_block', 'cache_filter', 'cache_page');
    $cache_tables = array_merge(module_invoke_all('flush_caches'), $core);
    foreach ($cache_tables as $table) {
        cache_clear_all('*', $table, TRUE);
    }
}
 
    注意包含module_invoke_all('flush_caches')的那行代码。这将触发hook_flush_caches()。如果你使用了自己的缓存表,那么当点击“管理➤站点配置 ➤性能”页面的“清除缓存数据”按钮时,通过实现钩子hook_flush_caches(),你的模块就可以清除自己的缓存了。该按钮的提交处理器调用的就是drupal_flush_all_caches()。hook_flush_caches()的实现非常简单;你的模块只需要简单得返回一个数组,里面包含要被清空的缓存表就可以了。下面是来自更新状态模块的例子:
 
/**
 * Implementation of hook_flush_caches().
 */
function update_flush_caches() {
    return array('cache_update');
}

老葛的Drupal培训班 Think in Drupal

Drupal版本: