Index: Drupal_Apache_Solr_Service.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/Drupal_Apache_Solr_Service.php,v
retrieving revision 1.1.2.16
diff -u -p -r1.1.2.16 Drupal_Apache_Solr_Service.php
--- Drupal_Apache_Solr_Service.php	13 Apr 2009 14:51:15 -0000	1.1.2.16
+++ Drupal_Apache_Solr_Service.php	28 Apr 2009 02:49:24 -0000
@@ -58,7 +58,7 @@ class Drupal_Apache_Solr_Service extends
     if (empty($this->luke[$num_terms])) {
       $url = $this->_constructUrl(self::LUKE_SERVLET, array('numTerms' => "$num_terms", 'wt' => self::SOLR_WRITER));
       $this->luke[$num_terms] = $this->_sendRawGet($url);
-      cache_set($this->luke_cid, $this->luke);
+      cache_set($this->luke_cid, $this->luke, 'cache_apachesolr');
     }
   }
 
@@ -88,14 +88,14 @@ class Drupal_Apache_Solr_Service extends
     if (empty($this->stats) && isset($data->index->numDocs)) {
       $url = $this->_constructUrl(self::STATS_SERVLET);
       $this->stats_cid = "apachesolr:stats:" . md5($url);
-      $cache = cache_get($this->stats_cid);
+      $cache = cache_get($this->stats_cid, 'cache_apachesolr');
       if (isset($cache->data)) {
         $this->stats = simplexml_load_string($cache->data);
       }
       else {
         $response = $this->_sendRawGet($url);
         $this->stats = simplexml_load_string($response->getRawResponse());
-        cache_set($this->stats_cid, $response->getRawResponse());
+        cache_set($this->stats_cid, $response->getRawResponse(), 'cache_apachesolr');
       }
     }
   }
@@ -148,8 +148,7 @@ class Drupal_Apache_Solr_Service extends
    * Clear cached Solr data.
    */
   public function clearCache() {
-    cache_clear_all("apachesolr:luke:", 'cache', TRUE);
-    cache_clear_all("apachesolr:stats:", 'cache', TRUE);
+    cache_clear_all("*:", 'cache_apachesolr', TRUE);
     $this->luke = array();
     $this->stats = NULL;
   }
@@ -181,13 +180,51 @@ class Drupal_Apache_Solr_Service extends
    */
   public function __construct($host = 'localhost', $port = 8180, $path = '/solr/') {
     parent::__construct($host, $port, $path);
-    $this->luke_cid = "apachesolr:luke:$host:$port:$path";
-    $cache = cache_get($this->luke_cid);
+    $this->luke_cid = "apachesolr:luke:" . md5($this->_lukeUrl);
+    $cache = cache_get($this->luke_cid, 'cache_apachesolr');
     if (isset($cache->data)) {
       $this->luke = $cache->data;
     }
   }
 
+
+  /**
+   * Simple Search interface - extending the parent one to add caching.
+   *
+   * @param string $query The raw query string.
+   * @param int $offset The starting offset for result documents.
+   * @param int $limit The maximum number of result documents to return.
+   * @param array $params key / value pairs for other query parameters.
+   *        (see Solr documentation), use arrays for parameter keys used
+   *         more than once (e.g. facet.field).
+   * @param bool $cache_result (optional) should the result be cached (when appropriate).
+   *
+   * @return Apache_Solr_Response
+   *
+   * @throws Exception If an error occurs during the service call
+   */
+  public function search($query, $offset = 0, $limit = 10, $params = array(), $cache_result = FALSE) {
+    $do_cache = $cache_result && (CACHE_DISABLED == variable_get('cache', CACHE_DISABLED));
+
+    if ($do_cache) {
+     // Check for cached data.
+     $cid = 'query:' . sha1($this->_searchUrl . $query . serialize($params)) .':'. $offset .':'. $limit;
+     $cache = cache_get($cid, 'cache_apachesolr');
+     if (isset($cache->data)) {
+       return $cache->data;
+     }
+    }
+
+    $result = parent::search($query, $offset, $limit, $params);
+
+    if ($do_cache) {
+     // Handle data caching.
+     cache_set($cid, $result, 'cache_apachesolr');
+    }
+
+    return $result;
+  }
+
   /**
    * Central method for making a get operation against this Solr Server
    *
Index: apachesolr.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/apachesolr.module,v
retrieving revision 1.1.2.12.2.133
diff -u -p -r1.1.2.12.2.133 apachesolr.module
--- apachesolr.module	24 Apr 2009 18:03:24 -0000	1.1.2.12.2.133
+++ apachesolr.module	28 Apr 2009 02:49:24 -0000
@@ -282,6 +282,8 @@ function apachesolr_index_nodes($result,
       foreach ($docs_chunk as $docs) {
         $solr->addDocuments($docs);
       }
+      // Set flag to indicate index update.
+      variable_set('apachesolr_index_updated', TRUE);
     }
     catch (Exception $e) {
       watchdog('Apache Solr', $e->getMessage(), NULL, WATCHDOG_ERROR);
@@ -324,19 +326,17 @@ function apachesolr_delete_node_from_ind
  */
 function apachesolr_cron() {
   try {
+    $index_updated = FALSE;
     $solr = apachesolr_get_solr();
-    $solr->clearCache();
-    // Re-populate the luke cache.
-    $solr->getLuke();
     // Check for unpublished content that wasn't deleted from the index.
     $result = db_query("SELECT n.nid, n.status FROM {apachesolr_search_node} asn INNER JOIN {node} n ON n.nid = asn.nid WHERE asn.status != n.status");
     while ($node = db_fetch_object($result)) {
-      _apachesolr_nodeapi_update($node, FALSE);
+      $index_updated = $index_updated || _apachesolr_nodeapi_update($node, FALSE);
     }
     // Check for deleted content that wasn't deleted from the index.
     $result = db_query("SELECT asn.nid FROM {apachesolr_search_node} asn LEFT JOIN {node} n ON n.nid = asn.nid WHERE n.nid IS NULL");
     while ($node = db_fetch_object($result)) {
-      _apachesolr_nodeapi_delete($node, FALSE);
+      $index_updated = $index_updated || _apachesolr_nodeapi_delete($node, FALSE);
     }
     $last = variable_get('apachesolr_last_optimize', 0);
     $time = time();
@@ -344,6 +344,15 @@ function apachesolr_cron() {
     if ($time - $last > 60*60*24) {
       $solr->optimize(FALSE, FALSE);
       variable_set('apachesolr_last_optimize', $time);
+      $index_updated = TRUE;
+    }
+    // Only clear the cache if the index changed, but at least once daily.
+    // TODO: clear on some scheule if running multi-site.
+    if ($index_updated || variable_get('apachesolr_index_updated', FALSE)) {
+      $solr->clearCache();
+      // Re-populate the luke cache.
+      $solr->getLuke();
+      variable_del('apachesolr_index_updated');
     }
   }
   catch (Exception $e) {
@@ -352,6 +361,13 @@ function apachesolr_cron() {
 }
 
 /**
+ * Implementation of hook_flush_caches().
+ */
+function apachesolr_flush_caches() {
+  return array('cache_apachesolr');
+}
+
+/**
  * Implementation of hook_nodeapi().
  */
 function apachesolr_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
@@ -372,24 +388,34 @@ function apachesolr_nodeapi(&$node, $op,
 
 /**
  * Helper function for hook_nodeapi() and hook_cron().
+ *
+ * @return
+ *   TRUE if the Solr index was changed.
  */
 function _apachesolr_nodeapi_delete($node, $set_message = TRUE) {
-  if (apachesolr_delete_node_from_index($node)) {
+  $index_updated = apachesolr_delete_node_from_index($node);
+  if ($index_updated) {
     // There was no exception, so delete from the table.
     db_query("DELETE FROM {apachesolr_search_node} WHERE nid = %d", $node->nid);
     if ($set_message && user_access('administer site configuration') && variable_get('apachesolr_set_nodeapi_messages', 1)) {
       apachesolr_set_stats_message('Deleted content will be removed from the Apache Solr search index in approximately @autocommit_time.');
     }
   }
+  return $index_updated;
 }
 
 /**
  * Helper function for hook_nodeapi() and hook_cron().
+ *
+ * @return
+ *   TRUE if the Solr index was changed. 
  */
 function _apachesolr_nodeapi_update($node, $set_message = TRUE) {
+  $index_updated = FALSE;
   // Check if the node has gone from published to unpublished.
   if (!$node->status && db_result(db_query("SELECT status FROM {apachesolr_search_node} WHERE nid = %d", $node->nid))) {
-    if (apachesolr_delete_node_from_index($node)) {
+    $index_updated = apachesolr_delete_node_from_index($node);
+    if ($index_updated) {
       // There was no exception, so update the table.
       db_query("UPDATE {apachesolr_search_node} SET changed = %d, status = %d WHERE nid  = %d", time(), $node->status, $node->nid);
       if ($set_message && user_access('administer site configuration') && variable_get('apachesolr_set_nodeapi_messages', 1)) {
@@ -400,6 +426,7 @@ function _apachesolr_nodeapi_update($nod
   else {
     db_query("UPDATE {apachesolr_search_node} SET changed = %d, status = %d WHERE nid  = %d", time(), $node->status, $node->nid);
   }
+  return $index_updated;
 }
 
 /**
Index: apachesolr.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/apachesolr.install,v
retrieving revision 1.1.4.15
diff -u -p -r1.1.4.15 apachesolr.install
--- apachesolr.install	14 Apr 2009 00:12:18 -0000	1.1.4.15
+++ apachesolr.install	28 Apr 2009 02:49:24 -0000
@@ -63,6 +63,9 @@ function apachesolr_schema() {
       ),
     'primary key' => array('nid'),
     );
+ 
+  $schema['cache_apachesolr'] = drupal_get_schema_unprocessed('system', 'cache');
+  $schema['cache_apachesolr']['description'] = 'Cache table for the Apache Solr integration module or depdent modules to store index data and query results.';
 
   return $schema;
 }
@@ -160,3 +163,18 @@ function apachesolr_update_6003() {
   }
   return $ret;
 }
+
+/**
+ * Add new cache table.
+ */
+function apachesolr_update_6004() {
+  $ret = array();
+
+  $schema['cache_apachesolr'] = drupal_get_schema_unprocessed('system', 'cache');
+  $schema['cache_apachesolr']['description'] = 'Cache table for the Apache Solr integration module or depdent modules to store index data and query results.';
+
+  db_create_table($ret, 'cache_apachesolr', $schema['cache_apachesolr']);
+
+  return $ret;
+}
+
Index: contrib/apachesolr_mlt/apachesolr_mlt.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/contrib/apachesolr_mlt/Attic/apachesolr_mlt.module,v
retrieving revision 1.1.4.27
diff -u -p -r1.1.4.27 apachesolr_mlt.module
--- contrib/apachesolr_mlt/apachesolr_mlt.module	31 Mar 2009 16:06:37 -0000	1.1.4.27
+++ contrib/apachesolr_mlt/apachesolr_mlt.module	28 Apr 2009 02:49:24 -0000
@@ -92,7 +92,7 @@ function apachesolr_mlt_suggestions($blo
       return;
     }
 
-    $response = $solr->search($query->get_query_basic(), 0, $block['num_results'], $params);
+    $response = $solr->search($query->get_query_basic(), 0, $block['num_results'], $params, TRUE);
     $suggestions = array();
     if ($response->response) {
       $docs = (array) end($response->response);
