Index: apachesolr.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/apachesolr.admin.inc,v
retrieving revision 1.1.2.6
diff -u -p -r1.1.2.6 apachesolr.admin.inc
--- apachesolr.admin.inc	28 Jan 2009 13:53:03 -0000	1.1.2.6
+++ apachesolr.admin.inc	3 Feb 2009 00:34:31 -0000
@@ -71,7 +71,7 @@ function apachesolr_index_page() {
     $solr = apachesolr_get_solr();
     // TODO: only clear this every page view if we are running
     // multi-site.
-    $solr->clearCache();
+    $solr->clearLukeCache();
     // Note: we use 2 since 1 fails on Ubuntu Hardy.
     $data = $solr->getLuke(2);
   }
Index: apachesolr.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/apachesolr.install,v
retrieving revision 1.1.4.12
diff -u -p -r1.1.4.12 apachesolr.install
--- apachesolr.install	28 Jan 2009 19:28:32 -0000	1.1.4.12
+++ apachesolr.install	3 Feb 2009 00:34:31 -0000
@@ -60,6 +60,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;
 }
 
@@ -131,5 +134,18 @@ function apachesolr_update_6001() {
   $ret[] = update_sql("UPDATE {blocks} set cache = " . BLOCK_CACHE_PER_PAGE . " WHERE module LIKE 'apachesolr%'");
 
   return $ret;
-  
+}
+
+/**
+ * Add new cache table.
+ */
+function apachesolr_update_6002() {
+  $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: apachesolr.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/apachesolr.module,v
retrieving revision 1.1.2.12.2.99
diff -u -p -r1.1.2.12.2.99 apachesolr.module
--- apachesolr.module	28 Jan 2009 13:49:46 -0000	1.1.2.12.2.99
+++ apachesolr.module	3 Feb 2009 00:34:31 -0000
@@ -338,7 +338,7 @@ function apachesolr_node_to_document($ni
     $document->nid = $node->nid;
     $document->status = $node->status;
     $document->uid = $node->uid;
-    $document->title = $node->title;
+    $document->title = $node->title; // Already cleaned above.
     $document->body = strip_tags($text);
     $document->type  = $node->type;
     $document->type_name = apachesolr_strip_ctl_chars(node_get_types('name', $node));
@@ -502,7 +502,7 @@ function apachesolr_delete_node_from_ind
 function apachesolr_cron() {
   try {
     $solr = apachesolr_get_solr();
-    $solr->clearCache();
+    $solr->clearLukeCache();
     $last = variable_get('apachesolr_last_optimize', 0);
     $time = time();
     // Make sure to omtimize once per day.
@@ -517,6 +517,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) {
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.9
diff -u -p -r1.1.2.9 Drupal_Apache_Solr_Service.php
--- Drupal_Apache_Solr_Service.php	11 Jan 2009 03:21:01 -0000	1.1.2.9
+++ Drupal_Apache_Solr_Service.php	3 Feb 2009 00:34:31 -0000
@@ -14,7 +14,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');
     }
   }
 
@@ -36,14 +36,31 @@ class Drupal_Apache_Solr_Service extends
   }
 
   /**
-   * Clear cached Solr data.
+   * Clear all cached Solr data.
    */
   public function clearCache() {
-    cache_clear_all("apachesolr:luke:", 'cache', TRUE);
+    $this->clearLukeCache();
+    $this->clearQueryCache();
+  }
+
+  /**
+   * Clear all cached Solr luke data.
+   */
+  public function clearLukeCache() {
+    // Clear all luke data.
+    cache_clear_all('luke:', 'cache_apachesolr', TRUE);
     $this->luke = array();
   }
 
   /**
+   * Clear all cached Solr query data.
+   */
+  public function clearQueryCache() {
+    // Clear all cached query data.
+    cache_clear_all('query:', 'cache_apachesolr', TRUE);
+  }
+
+  /**
    * Clear the cache whenever we commit changes.
    *
    * @see Apache_Solr_Service::commit()
@@ -70,8 +87,8 @@ 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 = "luke:$host:$port:$path";
+    $cache = cache_get($this->luke_cid, 'cache_apachesolr');
     if (isset($cache->data)) {
       $this->luke = $cache->data;
     }
@@ -148,4 +165,41 @@ class Drupal_Apache_Solr_Service extends
     }
     return array($result->data, $headers);
   }
+
+  /**
+   * 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 int $cache_lifetime (optional) cache lifetime in seconds.
+   *
+   * @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_lifetime = 0) {
+    $do_cache = (is_numeric($cache_lifetime) && $cache_lifetime > 0);
+
+    if ($do_cache) {
+     // Check for cached data.
+     $cid = 'query:' . sha1($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', $_SERVER['REQUEST_TIME'] + $cache_lifetime);
+    }
+
+    return $result;
+  }
 }
