Index: includes/menu.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/menu.inc,v
retrieving revision 1.255.2.9
diff -u -p -r1.255.2.9 menu.inc
--- includes/menu.inc	27 Feb 2008 12:12:01 -0000	1.255.2.9
+++ includes/menu.inc	9 Mar 2008 21:22:05 -0000
@@ -773,13 +773,17 @@ function menu_tree_all_data($menu_name =
   // Use $mlid as a flag for whether the data being loaded is for the whole tree.
   $mlid = isset($item['mlid']) ? $item['mlid'] : 0;
   // Generate the cache ID.
-  $cid = 'links:'. $menu_name .':all:'. $mlid;
+  $cid = 'links-cid:'. $menu_name .':all:'. $mlid;
 
   if (!isset($tree[$cid])) {
     // If the static variable doesn't have the data, check {cache_menu}.
     $cache = cache_get($cid, 'cache_menu');
     if ($cache && isset($cache->data)) {
-      $data = $cache->data;
+      // The first entry is just the cid for the real data, to avoid duplication
+      $cache = cache_get($cache->data, 'cache_menu');
+      if ($cache && isset($cache->data)) {
+        $data = $cache->data;
+      }
     }
     else {
       // Build and run the query, and build the tree.
@@ -813,8 +817,14 @@ function menu_tree_all_data($menu_name =
         ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args), $parents);
       $data['node_links'] = array();
       menu_tree_collect_node_links($data['tree'], $data['node_links']);
-      // Cache the data.
-      cache_set($cid, $data, 'cache_menu');
+      // Cache the data, or just a reference to the already cached data.
+      $tree_cid = _menu_tree_cid($data);
+      if (_menu_tree_is_cached($tree_cid)) {
+        cache_set($cid, $tree_cid, 'cache_menu');
+      }
+      else {
+        cache_set($tree_cid, $data, 'cache_menu');
+      }
     }
     // Check access for the current user to each item in the tree.
     menu_tree_check_access($data['tree'], $data['node_links']);
@@ -845,13 +855,17 @@ function menu_tree_page_data($menu_name 
   // Load the menu item corresponding to the current page.
   if ($item = menu_get_item()) {
     // Generate the cache ID.
-    $cid = 'links:'. $menu_name .':page:'. $item['href'] .':'. (int)$item['access'];
+    $cid = 'links-cid:'. $menu_name .':page:'. $item['href'] .':'. (int)$item['access'];
 
     if (!isset($tree[$cid])) {
       // If the static variable doesn't have the data, check {cache_menu}.
       $cache = cache_get($cid, 'cache_menu');
       if ($cache && isset($cache->data)) {
-        $data = $cache->data;
+        // The first entry is just the cid for the real data, to avoid duplication
+        $cache = cache_get($cache->data, 'cache_menu');
+        if ($cache && isset($cache->data)) {
+          $data = $cache->data;
+        }
       }
       else {
         // Build and run the query, and build the tree.
@@ -909,8 +923,14 @@ function menu_tree_page_data($menu_name 
           ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args), $parents);
         $data['node_links'] = array();
         menu_tree_collect_node_links($data['tree'], $data['node_links']);
-        // Cache the data.
-        cache_set($cid, $data, 'cache_menu');
+        // Cache the data, or just a reference to the already cached data.
+        $tree_cid = _menu_tree_cid($data);
+        if (_menu_tree_is_cached($tree_cid)) {
+          cache_set($cid, $tree_cid, 'cache_menu');
+        }
+        else {
+          cache_set($tree_cid, $data, 'cache_menu');
+        }
       }
       // Check access for the current user to each item in the tree.
       menu_tree_check_access($data['tree'], $data['node_links']);
@@ -923,6 +943,21 @@ function menu_tree_page_data($menu_name 
 }
 
 /**
+ * Helper function - compute the real cache ID for menu tree data.
+ */
+function _menu_tree_cid($data) {
+  return 'tree:'. md5(serialize($data));
+}
+
+/**
+ * Helper function - check if a menu tree is already cached.
+ */
+function _menu_tree_is_cached($tree_cid) {
+  return (bool)db_result(db_query("SELECT cid FROM {cache_menu} WHERE cid = '%s'", $tree_cid));
+}
+
+
+/**
  * Recursive helper function - collect node links.
  */
 function menu_tree_collect_node_links(&$tree, &$node_links) {
