TPAC cache copy location groups user/berick/tpac-cache-copy-loc-groups
authorBill Erickson <berick@esilibrary.com>
Wed, 21 Aug 2013 20:57:15 +0000 (16:57 -0400)
committerBill Erickson <berick@esilibrary.com>
Wed, 21 Aug 2013 20:57:15 +0000 (16:57 -0400)
Like other similary types of data, which are loaded on every page load,
cache copies of the copy location groups (by locale and org unit
context) in a process-local cache to speed up page loads and reduce DB
calls.

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Util.pm

index 6382210..247ecc8 100644 (file)
@@ -4,6 +4,7 @@ use Apache2::Const -compile => qw(OK DECLINED FORBIDDEN HTTP_INTERNAL_SERVER_ERR
 use File::Spec;
 use Time::HiRes qw/time sleep/;
 use OpenSRF::Utils::Cache;
+use OpenSRF::Utils::JSON;
 use OpenSRF::Utils::Logger qw/$logger/;
 use OpenILS::Utils::CStoreEditor qw/:funcs/;
 use OpenILS::Utils::Fieldmapper;
@@ -22,7 +23,8 @@ our %cache = ( # cached data
     aou_tree => {en_us => undef},
     aouct_tree => {},
     eg_cache_hash => undef,
-    authority_fields => {en_us => {}}
+    authority_fields => {en_us => {}},
+    copy_location_groups => {en_us => {}}
 );
 
 sub init_ro_object_cache {
@@ -565,6 +567,12 @@ sub load_copy_location_groups {
     push(@ctx_orgs, $ctx->{physical_loc}) if $ctx->{physical_loc};
     push(@ctx_orgs, $ctx->{user}->home_ou) if $ctx->{user};
 
+    @ctx_orgs = sort(keys %{{ map {$_ => 1} @ctx_orgs}}); # sort/uniq
+    my $cache_key = OpenSRF::Utils::JSON->perl2JSON(\@ctx_orgs);
+
+    return $cache{copy_location_groups}{$ctx->{locale}}{$cache_key}
+        if $cache{copy_location_groups}{$ctx->{locale}}{$cache_key};
+
     my $grps = $self->editor->search_asset_copy_location_group([
         {
             opac_visible => 't',
@@ -585,7 +593,10 @@ sub load_copy_location_groups {
 
     my %buckets;
     push(@{$buckets{$_->owner}}, $_) for @$grps;
-    $ctx->{copy_location_groups} = \%buckets;
+
+    $ctx->{copy_location_groups} = 
+        $cache{copy_location_groups}{$ctx->{locale}}{$cache_key} =
+        \%buckets;
 }
 
 sub set_file_download_headers {