--- acidfree.module	Tue Jan 23 09:10:34 2007
+++ acidfree.module	Sun Feb  4 03:46:26 2007
@@ -1295,6 +1295,8 @@
     global $ACIDFREE_SYNC_PRIVATE;
     global $acidfree_types;
 
+   _acidfree_decache_album_tree_cached_root();
+
     // promote the files from working to active
     foreach (Array('large', 'small', 'thumb') as $size) {
         $fid = (is_object($node->$size) ? $node->$size->fid : $node->$size);
@@ -1343,7 +1345,8 @@
       if ($root->nid) {
         $valid[$root->nid] = 1;
       }
-      $tree = acidfree_get_album_tree($root);
+      _acidfree_decache_album_tree_cached_root();
+      $tree = _acidfree_get_album_tree_cached_root($root);
       if (count($tree) > 1) {
         foreach ($tree as $album) {
           // TODO: I am not sure if duplicates are ever possible, but assume they are and drop them as the come
@@ -1398,6 +1401,9 @@
  */
 function acidfree_delete(&$node) {
     global $acidfree_types;
+
+    _acidfree_decache_album_tree_cached_root();
+	
     acidfree_call($acidfree_types[$node->class]->destroy, array(&$node));
     db_query("DELETE FROM {acidfree} WHERE aid = %d", $node->nid);
     db_query("DELETE FROM {acidfree_hierarchy} WHERE child = %d", $node->nid);
@@ -2009,6 +2015,33 @@
     }
 }
 
+function _acidfree_get_album_tree_cached_root($root){
+    
+    $tcrttl=10*60; // 10 min
+
+    if ((time()  - variable_get('acidfree_root_tree_array_time', 0)) < $tcrttl) {
+	$variable_cached_tree = variable_get('acidfree_root_tree_array', null);
+    } else {
+	$variable_cached_tree = null;
+    }
+    
+    if ( ! $variable_cached_tree) {
+	$tree = acidfree_get_album_tree($root);
+	variable_set('acidfree_root_tree_array', serialize($tree));
+	variable_set('acidfree_root_tree_array_time', time());
+    } else {
+	$tree = unserialize($variable_cached_tree);
+	$tree[0]=$root;
+    }
+    return $tree;
+}
+
+function _acidfree_decache_album_tree_cached_root(){
+
+    variable_del("acidfree_root_tree_array");
+    
+}
+
 function _acidfree_parent_select($title, $value, $description, $multiple, $exclude = array(), $extra = array()) {
     global $user;
     $any_album = user_access('can upload to any album');
@@ -2016,7 +2049,7 @@
     $root->title = '<'.t('Root').'>';
     if (variable_get('acidfree_per_user_albums', 0))
         $my_root = acidfree_get_user_root($user->uid);
-    $tree = acidfree_get_album_tree($root);
+    $tree = _acidfree_get_album_tree_cached_root($root);
     if (!$value) {
         $value = array($root->nid);
     }
