Index: src/edu/ucsb/nceas/metacat/DBQuery.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DBQuery.java	(revision 3341)
+++ src/edu/ucsb/nceas/metacat/DBQuery.java	(revision 3342)
@@ -81,7 +81,7 @@
     Vector docidOverride = new Vector();
     
     // a hash table serves as query reuslt cache. Key of hashtable
-    // is a query string and value is ResultDocumentSet Object
+    // is a query string and value is result xml string
     private static Hashtable queryResultCache = new Hashtable();
     
     // Capacity of the query result cache
@@ -393,7 +393,6 @@
     HttpSession sess = (HttpSession)sessionHash.get(sessionid);
 
     
-    
     resultset.append("<?xml version=\"1.0\"?>\n");
     resultset.append("<resultset>\n");
     resultset.append("  <pagestart>" + pagestart + "</pagestart>\n");
@@ -418,9 +417,10 @@
 
         //print out the search result
         // search the doc list
-        resultset = findResultDoclist(qspec, resultset, out, user, groups,
+        StringBuffer resultContent = findResultDoclist(qspec, out, user, groups,
                                       dbconn, useXMLIndex, pagesize, pagestart, 
                                       sessionid);
+        resultset.append(resultContent);
       } //try
       catch (IOException ioe)
       {
@@ -459,7 +459,6 @@
      * Find the doc list which match the query
      */
     private StringBuffer findResultDoclist(QuerySpecification qspec,
-                                      StringBuffer resultsetBuffer,
                                       PrintWriter out,
                                       String user, String[]groups,
                                       DBConnection dbconn, boolean useXMLIndex,
@@ -466,6 +465,7 @@
                                       int pagesize, int pagestart, String sessionid)
                                       throws Exception
     {
+      StringBuffer resultsetBuffer = new StringBuffer();
       String query = null;
       int count = 0;
       int index = 0;
@@ -539,6 +539,24 @@
         
       }
       logMetacat.warn("============ final selection query: " + query);
+      
+      // we only get cache for public
+      if (user != null && user.equalsIgnoreCase("public") 
+     		 && pagesize == 0 && MetaCatUtil.getOption("query_cache_on").equals("true"))
+      {
+   	      String cachedResult = getResultXMLFromCache(query);
+   	      //System.out.println("==========the string from cache is "+cachedResult);
+   	      if (cachedResult != null)
+   	      {
+   	    	 if (out != null)
+   	         {
+   	             out.println(cachedResult);
+   	         }
+   	    	 resultsetBuffer.append(cachedResult);
+   	    	 return resultsetBuffer;
+   	      }
+      }
+      
       startTime = System.currentTimeMillis() / 1000;
       pstmt = dbconn.prepareStatement(query);
       rs = pstmt.executeQuery();
@@ -554,8 +572,8 @@
       
       if(pagesize == 0)
       { //this makes sure we get all results if there is no paging
-        pagesize = 99999;
-        pagestart = 99999;
+        pagesize = 9999999;
+        pagestart = 9999999;
       } 
       
       int currentIndex = 0;
@@ -638,7 +656,7 @@
        {
          ResultDocumentSet pagedResultsHash = new ResultDocumentSet();
          //get the last page of information then break
-         if(pagesize != 99999)
+         if(pagesize != 9999999)
          {
            for(int i=pagesize*pagestart; i<docListResult.size(); i++)
            {
@@ -663,6 +681,7 @@
      //if docListResult is not empty, it need to be sent.
      if (docListResult.size() != 0)
      {
+      
        handleSubsetResult(qspec,resultsetBuffer, out, docListResult,
                               user, groups,dbconn, useXMLIndex);
      }
@@ -672,6 +691,14 @@
      {
          out.println("\n<lastpage>" + lastpage + "</lastpage>\n");
      }
+     
+     // now we only cached none-paged query and user is public
+     if (user != null && user.equalsIgnoreCase("public") 
+    		 && pagesize == 9999999 && MetaCatUtil.getOption("query_cache_on").equals("true"))
+     {
+       //System.out.println("the string stored into cache is "+ resultsetBuffer.toString());
+  	   storeQueryResultIntoCache(query, resultsetBuffer.toString());
+     }
           
      return resultsetBuffer;
     }//findReturnDoclist
@@ -1251,11 +1278,11 @@
    }
    
    /*
-    * Method to store query string and DocumentResultSet into query result
+    * Method to store query string and result xml string into query result
     * cache. If the size alreay reache the limitation, the cache will be
     * cleared first, then store them.
     */
-   private void storeQueryResultIntoCache(String query, ResultDocumentSet resultSet)
+   private void storeQueryResultIntoCache(String query, String resultXML)
    {
 	   synchronized (queryResultCache)
 	   {
@@ -1263,23 +1290,23 @@
 		   {
 			   queryResultCache.clear();
 		   }
-		   queryResultCache.put(query, resultSet);
+		   queryResultCache.put(query, resultXML);
 		   
 	   }
    }
    
    /*
-    * Method to get DocumentResultSet from query result cache. 
-    * Note: the returned ResultDoucmentSet can be null.
+    * Method to get result xml string from query result cache. 
+    * Note: the returned string can be null.
     */
-   private ResultDocumentSet getResultDocumentSetFromCache(String query)
+   private String getResultXMLFromCache(String query)
    {
-	   ResultDocumentSet resultSet = null;
+	   String resultSet = null;
 	   synchronized (queryResultCache)
 	   {
           try
           {
-		     resultSet = (ResultDocumentSet)queryResultCache.get(query);
+		     resultSet = (String)queryResultCache.get(query);
 		   
           }
           catch (Exception e)
