http://drupal.org/node/557806 From: andrew morton Cache the toolbar per-user --- modules/toolbar/toolbar.module | 17 +++++++++++++++-- 1 files changed, 15 insertions(+), 2 deletions(-) diff --git modules/toolbar/toolbar.module modules/toolbar/toolbar.module index 0ab6f87..df58af3 100644 --- modules/toolbar/toolbar.module +++ modules/toolbar/toolbar.module @@ -32,7 +32,7 @@ function toolbar_theme($existing, $type, $theme, $path) { /** * Implement hook_page_alter(). - * + * * Add admin toolbar to the page_top region automatically. */ function toolbar_page_alter(&$page) { @@ -58,6 +58,17 @@ function toolbar_preprocess_page(&$vars) { function toolbar_build() { global $user; + // When building the toolbar we need to fetch the entire admin menu tree and + // check the user's access to each menu link, an expensive process. To avoid + // this the toolbar is cached on a per-user basis because the access callbacks + // can use an arbitrary criteria unrelated to roles or permissions. Since the + // toolbar is generally only accessible to a limited number of users, and is + // rendered on every page, it should still be more efficient than building it + // each time. + if ($cached = cache_get('toolbar:user:' . $user->uid, 'cache_block')) { + return $cached->data; + } + $module_path = drupal_get_path('module', 'toolbar'); $build = array( '#theme' => 'toolbar', @@ -104,6 +115,8 @@ function toolbar_build() { '#attributes' => array('id' => 'toolbar-shortcuts'), ); + cache_set('toolbar:user:' . $user->uid, $build, 'cache_block'); + return $build; } @@ -162,7 +175,7 @@ function toolbar_menu_navigation_links($tree) { /** * Checks whether an item is in the active trail. * - * Useful when using a menu generated by menu_tree_all_data() which does + * Useful when using a menu generated by menu_tree_all_data() which does * not set the 'in_active_trail' flag on items. * * @todo