[open-ils-commits] r9007 - trunk/Open-ILS/src/perlmods/OpenILS/Application/Search

svn at svn.open-ils.org svn at svn.open-ils.org
Thu Mar 13 22:22:37 EDT 2008


Author: erickson
Date: 2008-03-13 21:49:04 -0400 (Thu, 13 Mar 2008)
New Revision: 9007

Modified:
   trunk/Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm
Log:
added basic caching layer for staged search.  still need to do the number juggling to determine real limit/offset values

Modified: trunk/Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm
===================================================================
--- trunk/Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm	2008-03-14 01:10:04 UTC (rev 9006)
+++ trunk/Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm	2008-03-14 01:49:04 UTC (rev 9007)
@@ -738,6 +738,7 @@
 	signature	=> q/@see open-ils.search.biblio.multiclass.staged/);
 
 my $CACHE_LIMIT = 200;
+my $CHECK_LIMIT = 1000;
 
 sub staged_search {
 	my($self, $conn, $search_hash, $nocache) = @_;
@@ -749,20 +750,30 @@
     $method .= '.staff' if $self->api_name =~ /staff$/;
     $method .= '.atomic';
 
-    my $results = try_staged_search_cache($method, $search_hash);
+    $search_hash->{skip_check} ||= 0;
 
+    my ($hit_count, $results) = try_staged_search_cache($method, $search_hash);
+
     if($results) {
         $nocache = 1;
 
     } else {
         $results = $U->storagereq($method, %$search_hash);
-        unless($nocache) {
-            # XXX cache me
+        my $summary = shift(@$results);
+        $hit_count = $summary->{estimated_hit_count};
+
+        # Clean up the results
+        if($self->api_name =~ /biblio/) {
+            $results = [map {$_->{id}} @$results];
+        } else {
+            delete $_->{rel} for @$results;
         }
+            
+        cache_staged_search($method, $search_hash, $summary, $results) unless $nocache;
     }
 
     return {
-        summary => shift(@$results),
+        count => $hit_count,
         results => $results
     };
 }
@@ -770,12 +781,22 @@
 sub try_staged_search_cache {
     my $method = shift;
     my $search_hash = shift;
+
     my $key = search_cache_key($method, $search_hash);
     my $start = $search_hash->{offset};
     my $end = $start + $search_hash->{limit} - 1;
+    my $data = $cache->get_cache($key);
 
-    # XXX pull me from the cache
-    return undef;
+    $logger->info("searching search cache $key with skip_check $$search_hash{skip_check}");
+    return undef unless $data;
+    $logger->info("searching search cache $key with skip_check $$search_hash{skip_check}");
+    return undef unless $data = $data->{$$search_hash{skip_check}};
+    $logger->info("returning search cache $key with skip_check $$search_hash{skip_check}");
+
+    return (
+        $data->{summary}->{estimated_hit_count}, 
+        $data->{results}
+    );
 }
 
 # creates a unique token to represent the query in the cache
@@ -785,12 +806,26 @@
 	my @sorted;
     for my $key (sort keys %$search_hash) {
 	    push(@sorted, ($key => $$search_hash{$key})) 
-            if $key ne 'limit' and $key ne 'offset';
+            unless $key eq 'limit' or 
+                $key eq 'offset' or 
+                $key eq 'skip_check';
     }
 	my $s = OpenSRF::Utils::JSON->perl2JSON(\@sorted);
 	return $pfx . md5_hex($method . $s);
 }
 
+sub cache_staged_search {
+    my($method, $search_hash, $summary, $results) = @_;
+    my $cache_key = search_cache_key($method, $search_hash);
+    my $data = $cache->get_cache($cache_key);
+    $data ||= {};
+    $data->{$search_hash->{skip_check}} = {
+        summary => $summary,
+        results => $results
+    };
+    $logger->info("cached ranged search with skip_check $$search_hash{skip_check} and key $cache_key");
+    $cache->put_cache($data);
+}
 
 sub search_cache {
 



More information about the open-ils-commits mailing list