From fa2c3152638299a0b44062bd07a1c344c0241e32 Mon Sep 17 00:00:00 2001 From: erickson Date: Wed, 24 Mar 2010 03:11:33 +0000 Subject: [PATCH] anonymous cache service for web caching git-svn-id: svn://svn.open-ils.org/ILS/trunk@15943 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../OpenILS/Application/Actor/Container.pm | 98 ++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Actor/Container.pm b/Open-ILS/src/perlmods/OpenILS/Application/Actor/Container.pm index de6986e79..12079d0c3 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Actor/Container.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Actor/Container.pm @@ -7,6 +7,10 @@ use Data::Dumper; use OpenSRF::EX qw(:try); use OpenILS::Utils::Fieldmapper; use OpenILS::Utils::CStoreEditor qw/:funcs/; +use OpenSRF::Utils::SettingsClient; +use OpenSRF::Utils::Cache; +use Digest::MD5 qw(md5_hex); +use OpenSRF::Utils::JSON; my $apputils = "OpenILS::Application::AppUtils"; my $U = $apputils; @@ -506,6 +510,100 @@ sub container_update { +__PACKAGE__->register_method( + method => "anon_cache", + api_name => "open-ils.actor.anon_cache.set_value", + signature => { + desc => q/ + Sets a value in the anon web cache. If the session key is + undefined, one will be automatically generated. + /, + params => [ + {desc => 'Session key', type => 'string'}, + { + desc => q/Field name. The name of the field in this cache session whose value to set/, + type => 'string' + }, + { + desc => q/The cached value. This can be any type of object (hash, array, string, etc.)/, + type => 'any' + }, + ], + return => { + desc => 'session key on success, undef on error', + type => 'string' + } + } +); + +__PACKAGE__->register_method( + method => "anon_cache", + api_name => "open-ils.actor.anon_cache.get_value", + signature => { + desc => q/ + Returns the cached data at the specified field within the specified cache session. + /, + params => [ + {desc => 'Session key', type => 'string'}, + { + desc => q/Field name. The name of the field in this cache session whose value to set/, + type => 'string' + }, + ], + return => { + desc => 'cached value on success, undef on error', + type => 'any' + } + } +); + +__PACKAGE__->register_method( + method => "anon_cache", + api_name => "open-ils.actor.anon_cache.delete_session", + signature => { + desc => q/ + Deletes a cache session. + /, + params => [ + {desc => 'Session key', type => 'string'}, + ], + return => { + desc => 'Session key', + type => 'string' + } + } +); + +sub anon_cache { + my($self, $conn, $ses_key, $field_key, $value) = @_; + + my $sc = OpenSRF::Utils::SettingsClient->new; + my $cache = OpenSRF::Utils::Cache->new('global'); + my $cache_timeout = $sc->config_value(cache => global => 'max_anon_cache_time') || 1800; # 30 minutes + my $cache_size = $sc->config_value(cache => global => 'max_anon_cache_size') || 102400; # 100k + + if($self->api_name =~ /delete_session/) { + + return $cache->delete_cache($ses_key); + + } elsif( $self->api_name =~ /set_value/ ) { + + $ses_key = md5_hex(time . rand($$)) unless $ses_key; + my $blob = $cache->get_cache($ses_key) || {}; + $blob->{$field_key} = $value; + return undef if + length(OpenSRF::Utils::JSON->perl2JSON($blob)) > $cache_size; # bytes, characters, whatever ;) + $cache->put_cache($ses_key, $blob, $cache_timeout); + return $ses_key; + + } else { + + my $blob = $cache->get_cache($ses_key) or return undef; + return $blob->{$field_key}; + } +} + + 1; -- 2.11.0