TPAC Library schema info - add some caching
authorDan Scott <dscott@laurentian.ca>
Mon, 28 Oct 2013 15:26:55 +0000 (11:26 -0400)
committerDan Scott <dscott@laurentian.ca>
Fri, 1 Nov 2013 20:31:13 +0000 (16:31 -0400)
Rather than hitting the database every time, cache our requests for a
period of time. It's unlikely that hours of operation or addresses are
going to change very often...

Signed-off-by: Dan Scott <dscott@laurentian.ca>
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Library.pm

index 18094dd..cbda48f 100644 (file)
@@ -1,11 +1,13 @@
 package OpenILS::WWW::EGCatLoader;
 use strict; use warnings;
 use Apache2::Const -compile => qw(OK DECLINED FORBIDDEN HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST);
+use OpenSRF::Utils::JSON;
 use OpenSRF::Utils::Logger qw/$logger/;
 use OpenILS::Utils::CStoreEditor qw/:funcs/;
 use OpenILS::Utils::Fieldmapper;
 use OpenILS::Application::AppUtils;
 my $U = 'OpenILS::Application::AppUtils';
+my $library_cache;
 
 # context additions: 
 #   library : aou object
@@ -21,8 +23,7 @@ sub load_library {
     my $lib_id = $ctx->{page_args}->[0];
     $lib_id = $self->_resolve_org_id_or_shortname($lib_id);
 
-    return Apache2::Const::HTTP_BAD_REQUEST 
-        unless $lib_id;
+    return Apache2::Const::HTTP_BAD_REQUEST unless $lib_id;
 
     my $aou = $ctx->{get_aou}->($lib_id);
     my $sname = $aou->parent_ou;
@@ -35,15 +36,30 @@ sub load_library {
     $self->timelog("got basic lib info");
 
     # Get mailing address
-    if ($aou->mailing_address) {
+    $library_cache ||= OpenSRF::Utils::Cache->new('global');
+    my $address_cache_key = "TPAC_aou_address_cache_$lib_id";
+    my $address = OpenSRF::Utils::JSON->JSON2perl($library_cache->get_cache($address_cache_key));
+
+    if ($address) {
+        $ctx->{mailing_address} = $address;
+    } elsif (!$address && $aou->mailing_address) {
         my $session = OpenSRF::AppSession->create("open-ils.actor");
         $ctx->{mailing_address} =
             $session->request('open-ils.actor.org_unit.address.retrieve',
             $aou->mailing_address)->gather(1);
+        $library_cache->put_cache($address_cache_key, OpenSRF::Utils::JSON->perl2JSON($ctx->{mailing_address}), 360);
     }
 
     # Get current hours of operation
-    my $hours = $self->editor->retrieve_actor_org_unit_hours_of_operation($lib_id);
+    my $hours_cache_key = "TPAC_aouhoo_cache_$lib_id";
+    my $hours = OpenSRF::Utils::JSON->JSON2perl($library_cache->get_cache($hours_cache_key));
+
+    if (!$hours) {
+        $hours = $self->editor->retrieve_actor_org_unit_hours_of_operation($lib_id);
+        if ($hours->dow_0_open) {
+            $library_cache->put_cache($hours_cache_key, OpenSRF::Utils::JSON->perl2JSON($hours), 360);
+        }
+    }
     if ($hours) {
         $ctx->{hours} = $hours;
         # Generate naive schema.org format