Index: modules/category/category.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/category/category.inc,v
retrieving revision 1.42.2.18
diff -u -r1.42.2.18 category.inc
--- modules/category/category.inc	30 Sep 2006 03:52:51 -0000	1.42.2.18
+++ modules/category/category.inc	4 Oct 2006 00:32:04 -0000
@@ -26,9 +26,9 @@
   if (!is_numeric($cid)) {
     return FALSE;
   }
-  $query = 'SELECT c.*, n.title FROM {category} c INNER JOIN {node} n ON c.cid = n.nid';
+  $query = 'c.*, n.title FROM {category} c INNER JOIN {node} n ON c.cid = n.nid';
 
-  return category_get_cached_item('get_category', $cid, $query, 'c.cid = %d', FALSE, $reset);
+  return category_get_cached_item('get_category', $cid, $query, 'c.cid', FALSE, $reset);
 }
 
 /**
@@ -147,8 +147,8 @@
     return FALSE;
   }
 
-  $query = 'SELECT n.nid FROM {node} n INNER JOIN {category} c ON n.nid = c.cid';
-  return category_get_cached_item('is_cat_or_cont', $nid, $query, 'n.nid = %d', FALSE, $reset);
+  $query = 'n.nid FROM {node} n INNER JOIN {category} c ON n.nid = c.cid';
+  return category_get_cached_item('is_cat_or_cont', $nid, $query, 'n.nid', FALSE, $reset);
 }
 
 /**
@@ -1405,8 +1405,8 @@
  *   The ID of the cached value to find.
  * @param $query
  *   The query to use when getting values from the database.
- * @param $where
- *   The WHERE clause of the query.
+ * @param $field
+ *   The name of the field used as the ID.
  * @param $order_by
  *   The ORDER BY clause of the query. Defaults to FALSE.
  * @param $reset
@@ -1415,10 +1415,11 @@
  * @return
  *   The value specified by $id, or FALSE if no value was found.
  */
-function category_get_cached_item($type, $id, $query, $where, $order_by = FALSE, $reset = FALSE) {
-  // The $overflow_count variable determines how many values this function
-  // will query for in one go. Tweak if necessary.
-  static $overflow_count = 20, $cache = array(), $overflow_from = array();
+function category_get_cached_item($type, $id, $query, $field, $order_by = FALSE, $reset = FALSE) {
+  // The $overflow_count variable determines how far to cache forward.
+  // Tweak if necessary.
+  static $overflow_count = 128;
+  static $cache = array();
 
   if (!isset($id) || $id === '') {
     return FALSE;
@@ -1426,30 +1427,19 @@
 
   if (!isset($cache[$type]) || $reset) {
     $cache[$type] = array();
-    $overflow_from[$type] = 0;
   }
 
   if (!isset($cache[$type][$id])) {
-    $result = db_query($query .' WHERE '. $where .($order_by ? ' ORDER BY '. $order_by : ''), $id);
-    if ($item = db_fetch_object($result)) {
-      $cache[$type][$id] = $item;
-    }
-
-    if ($overflow_from[$type] !== FALSE) {
-      $result = db_query_range($query, $overflow_from[$type], $overflow_count);
-      if (db_num_rows($result)) {
-        $i = 0;
-
-        while ($item = db_fetch_object($result)) {
-          $cache[$type][$item->$id] = $item;
-          $i++;
-        }
-
-        $overflow_from[$type] += $i;
-      }
-      else {
-        $overflow_from[$type] = FALSE;
-      }
+    $query = 'SELECT '.$field.' AS __cache_idx__, '.$query;
+    // This lets the database do a RANGE optimization.
+    $where = " WHERE $field >= $id AND $field < ".($id+$overflow_count);
+
+    $result = db_query($query . $where . ($order_by ? ' ORDER BY '. $order_by : ''));
+
+    while ($item = db_fetch_array($result)) {
+      $index = $item['__cache_idx__'];
+      unset($item['__cache_idx__']);
+      $cache[$type][$index] = (object)$item;
     }
   }
 
