--- tagadelic.module	2006-07-28 13:02:49.000000000 -0400
+++ tagadelic.module	2006-09-01 10:21:30.000000000 -0400
@@ -200,29 +200,38 @@ function tagadelic_get_weighted_tags($vi
   if (!is_array($vids) || count($vids) == 0) {
     return array();
   }
-
-  // Fetch tags
-  $result = db_query_range('SELECT COUNT(*) AS count, d.tid, d.name, d.vid FROM {term_data} d INNER JOIN {term_node} n ON d.tid = n.tid WHERE d.vid IN ('. substr(str_repeat('%d,', count($vids)), 0, -1) .') GROUP BY d.tid, d.name, d.vid ORDER BY count DESC', $vids, 0, $size);
-
-  // Find minimum and maximum log-count.
-  $tags = array();
-  $min = 1e9;
-  $max = -1e9;
-  while ($tag = db_fetch_object($result)) {
-    $tag->number_of_posts = $tag->count;
-    $tag->count = log($tag->count);
-    $min = min($min, $tag->count);
-    $max = max($max, $tag->count);
-    $tags[$tag->tid] = $tag;
-  }
-  // Note: we need to ensure the range is slightly too large to make sure even
-  // the largest element is rounded down.
-  $range = max(.01, $max - $min) * 1.0001;
-
-  foreach ($tags as $key => $value) {
-    $tags[$key]->weight = 1 + floor($steps * ($value->count - $min) / $range);
-  }
-
+  else {
+    // use the $vids as part of the cache $cid
+    $cid = 'tagadelic:' . implode(',', $vids);
+  }
+  $data = cache_get($cid);
+  $tags = unserialize($data->data);
+  if (empty($data)) {
+    // Fetch tags
+    $result = db_query_range('SELECT COUNT(*) AS count, d.tid, d.name, d.vid FROM {term_data} d INNER JOIN {term_node} n ON d.tid = n.tid WHERE d.vid IN ('. substr(str_repeat('%d,', count($vids)), 0, -1) .') GROUP BY d.tid, d.name ORDER BY count DESC', $vids, 0, $size);
+  
+    // Find minimum and maximum log-count.
+    $tags = array();
+    $min = 1e9;
+    $max = -1e9;
+    while ($tag = db_fetch_object($result)) {
+      $tag->number_of_posts = $tag->count;
+      $tag->count = log($tag->count);
+      $min = min($min, $tag->count);
+      $max = max($max, $tag->count);
+      $tags[$tag->tid] = $tag;
+    }
+    // Note: we need to ensure the range is slightly too large to make sure even
+    // the largest element is rounded down.
+    $range = max(.01, $max - $min) * 1.0001;
+  
+    foreach ($tags as $key => $value) {
+      $tags[$key]->weight = 1 + floor($steps * ($value->count - $min) / $range);
+    }
+    //store the $tags in cache to speed page loads. Expire every half hour.
+    cache_set($cid, serialize($tags), $expire = time() + 1800, $headers = NULL);
+ }  
+  
   list($sort, $order) = explode(',', variable_get('tagadelic_sort_order', 'title,asc'));
   switch ($sort) {
     case 'title':
