From f1ed667e24dc04adfa1d3d4d682afae702c3cec3 Mon Sep 17 00:00:00 2001 From: Jaswinder Singh Date: Thu, 11 Oct 2018 15:25:55 -0400 Subject: [PATCH] LP#1772680: Fixed issues related to Caching Signed-off-by: Jaswinder Singh Signed-off-by: Galen Charlton --- .../perlmods/lib/OpenILS/Application/EbookAPI.pm | 20 +++++-------- .../src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm | 35 +++++++++++++++++----- .../web/js/ui/default/opac/ebook_api/session.js | 25 ++++++++++++---- 3 files changed, 54 insertions(+), 26 deletions(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI.pm index 45b3179d47..10f8389c04 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI.pm @@ -208,7 +208,7 @@ sub check_session { return; } } else { - $logger->info("EbookAPI: check_session: Creating New Session ($vendor)"); + $logger->info("EbookAPI: check_session: Creating New Session for vendor: $vendor"); return start_session($self, $conn, $vendor, $ou); } } @@ -245,10 +245,8 @@ __PACKAGE__->register_method( ); sub _start_session { - my $authtoken = shift; my $vendor = shift; my $ou = shift; - $ou = $ou || 1; # default to top-level org unit my $module; @@ -279,15 +277,11 @@ sub _start_session { $handler = $module->new($args); # create new handler object $handler->initialize(); # set handler attributes $handler->do_client_auth(); # authorize client session against API, if applicable - - # Setup cache structure per vendor { authtoke => {'vendor' => {handler}} } - my $cache_value = {}; - $cache_value->{$vendor} = $handler; # our "session" is actually just our handler object, serialized and cached - $cache->put_cache($authtoken, $cache_value, $cache_timeout); + $cache->put_cache($handler->{session_id}, $handler, $cache_timeout); - $logger->info("EbookAPI: _start_session: New session created (authtoken: $authtoken) for '$vendor' and ebook session_id: " .$handler->{session_id}); + $logger->info("EbookAPI: _start_session: New session created for '$vendor' : " .$handler->{session_id}); return $handler->{session_id}; } @@ -295,10 +289,9 @@ sub _start_session { sub start_session { my $self = shift; my $conn = shift; - my $authtoken = shift; my $vendor = shift; my $ou = shift; - return _start_session($authtoken, $vendor, $ou); + return _start_session($vendor, $ou); } __PACKAGE__->register_method( @@ -809,7 +802,7 @@ __PACKAGE__->register_method( sub _get_patron_xacts { my ($xact_type, $auth, $session_id, $barcode) = @_; - $logger->info("EbookAPI: getting $xact_type for session_id: $session_id"); + $logger->info("EbookAPI: _get_patron_xacts: $xact_type for session_id: $session_id"); # verify that user is authenticated in EG my $e = new_editor(authtoken => $auth); @@ -819,10 +812,11 @@ sub _get_patron_xacts { } # Check if there is cached object before making calls to an API - $logger->info("EbookAPI: Calling cached session for ".$xact_type); + $logger->info("EbookAPI: _get_patron_xacts: Calling cached session for ".$xact_type); my $xact_value = _get_cache_value($session_id, $xact_type); if (defined($xact_value)) { + $logger->info("EbookAPI: _get_patron_xacts: Returning cached session for $xact_type :". $xact_value); return $xact_value; } diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm index 7646b6ad25..0594378722 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm @@ -9,6 +9,8 @@ use OpenSRF::EX qw/:try/; use OpenILS::Utils::DateTime qw/:datetime/; use OpenSRF::Utils::JSON; use OpenSRF::Utils::Logger qw/$logger/; +use OpenSRF::Utils::Cache; +use OpenSRF::Utils::SettingsClient; use OpenILS::Application::AppUtils; use OpenILS::Utils::CStoreEditor qw/:funcs/; use OpenILS::Utils::Fieldmapper; @@ -53,6 +55,8 @@ use constant { }; my $ebook_rbdigital_session_id = ''; +my $cache; +my $cache_timeout; sub new { my($class, $apache, $ctx) = @_; @@ -70,6 +74,11 @@ sub new { OpenILS::Utils::CStoreEditor->init; # just in case $self->editor(new_editor()); + $cache = OpenSRF::Utils::Cache->new; + + my $sclient = OpenSRF::Utils::SettingsClient->new(); + $cache_timeout = $sclient->config_value("apps", "open-ils.ebook_api", "app_settings", "cache_timeout" ) || 300; + return $self; } @@ -461,18 +470,21 @@ sub load_rbdigital_common { if (defined($self->editor->requestor)) { $logger->info("EbookAPI: load_rbdigital_common: Token: ". $self->ctx->{authtoken}); my $session_id = ""; - $logger->info("EbookAPI: load_rbdigital_common: COOKIE_EBOOK_RBDIGITAL: ".$self->cgi->cookie(COOKIE_EBOOK_RBDIGITAL)); - if ($self->cgi->cookie(COOKIE_EBOOK_RBDIGITAL)) { - $session_id = $self->cgi->cookie(COOKIE_EBOOK_RBDIGITAL); - $logger->info("EbookAPI: load_rbdigital_common: previous session_id: $session_id"); + + my $cached_session = $cache->get_cache($self->ctx->{authtoken}) || undef; + $logger->info("EbookAPI: load_rbdigital_common: Cached session result: ".Dumper($cached_session)); + if ($cached_session) { + $session_id = $cached_session; + $logger->info("EbookAPI: load_rbdigital_common: Retrieved cached session_id: $session_id"); + } else { + $session_id = _get_ebook_session_id($session_id, $self->editor->requestor->home_ou); + $logger->info("EbookAPI: load_rbdigital_common: Retrieved new session: $session_id"); + # Save session id into cache + $cache->put_cache($self->ctx->{authtoken}, $session_id, $cache_timeout); } - $logger->info("EbookAPI: load_rbdigital_common: cookie value for rbdigital session: $session_id"); - $session_id = _get_ebook_session_id($session_id, $self->editor->requestor->home_ou); $ebook_rbdigital_session_id = $session_id; - $logger->info("EbookAPI: load_rbdigital_common: COOKIE_EBOOK_RBDIGITAL: ". $self->cgi->cookie(COOKIE_EBOOK_RBDIGITAL)); - $logger->info("EbookAPI: load_rbdigital_common: after checking session id: $session_id"); $self->ctx->{rbdigital_session_id} = $session_id; $self->ctx->{vendors} = []; @@ -785,6 +797,13 @@ sub load_ebook_logout { _logout_session($session_id); } + # Lastly, delete cache for the authtoken + try { + $cache->delete_cache($self->ctx->{authtoken}); + } catch Error with { + + }; + } 1; diff --git a/Open-ILS/web/js/ui/default/opac/ebook_api/session.js b/Open-ILS/web/js/ui/default/opac/ebook_api/session.js index f1c1bf30fd..3fac7d8f4d 100755 --- a/Open-ILS/web/js/ui/default/opac/ebook_api/session.js +++ b/Open-ILS/web/js/ui/default/opac/ebook_api/session.js @@ -11,7 +11,10 @@ function startSession(vendor, callback) { if (resp) { var ses = resp.content(); dojo.cookie(vendor, ses,{path: '/'}); - ebook_sessions.rbdigital_session_id = ses; + if (vendor == 'rbdigital') { + ebook_sessions.rbdigital_session_id = ses; + } + return callback(vendor,ses); } } @@ -41,11 +44,23 @@ function checkSession(vendor, callback) { oncomplete: function(r) { var resp = r.recv(); if (resp) { - var new_ses = resp.content(); - dojo.cookie(vendor, new_ses, {path: '/'}); - ebook_sessions.rbdigital_session_id = new_ses; - return callback(vendor,new_ses); + var ses = resp.content(); + dojo.cookie(vendor, ses, {path: '/'}); + if (vendor == 'rbdigital') { + ebook_sessions.rbdigital_session_id = ses; + } + return callback(vendor,ses); } } }).send(); } + + +/* +function checkSession(vendor, callback) { + + var new_session = vendor + "_" + authtoken; + + return callback(vendor,new_session); +} +*/ \ No newline at end of file -- 2.11.0