TPAC Library schema info - add some caching
authorDan Scott <dscott@laurentian.ca>
Mon, 28 Oct 2013 15:26:55 +0000 (11:26 -0400)
committerDan Wells <dbw2@calvin.edu>
Tue, 21 Jan 2014 19:57:42 +0000 (14:57 -0500)
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>
Signed-off-by: Dan Wells <dbw2@calvin.edu>
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Library.pm

index 18094dd..d0b17a6 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;
@@ -34,26 +35,38 @@ sub load_library {
 
     $self->timelog("got basic lib info");
 
-    # Get mailing address
-    if ($aou->mailing_address) {
+    # Get mailing address from the cache
+    $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) {
+        # We didn't get cached hours, so hit the database
         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 we don't have cached hours, try the database
+    if (!$hours) {
+        $hours = $self->editor->retrieve_actor_org_unit_hours_of_operation($lib_id);
+        # If we got hours from the database, cache them
+        if ($hours) {
+            $library_cache->put_cache($hours_cache_key, OpenSRF::Utils::JSON->perl2JSON($hours), 360);
+        }
+    }
+
+    # After all that, if we have hours, pass them to the context object
     if ($hours) {
         $ctx->{hours} = $hours;
-        # Generate naive schema.org format
-        $ctx->{hours_schema} = "Mo " . substr($hours->dow_0_open, 0, 5) . "-" . substr($hours->dow_0_close, 0, 5) .
-            ",Tu " . substr($hours->dow_1_open, 0, 5) . "-" . substr($hours->dow_1_close, 0, 5) .
-            ",We " . substr($hours->dow_2_open, 0, 5) . "-" . substr($hours->dow_2_close, 0, 5) .
-            ",Th " . substr($hours->dow_3_open, 0, 5) . "-" . substr($hours->dow_3_close, 0, 5) .
-            ",Fr " . substr($hours->dow_4_open, 0, 5) . "-" . substr($hours->dow_4_close, 0, 5) .
-            ",Sa " . substr($hours->dow_5_open, 0, 5) . "-" . substr($hours->dow_5_close, 0, 5) .
-            ",Su " . substr($hours->dow_6_open, 0, 5) . "-" . substr($hours->dow_6_close, 0, 5);
     }
 
     return Apache2::Const::OK;