From 0c9fdc92f3c38e93cc928f7818bbe6e516bb98bc Mon Sep 17 00:00:00 2001 From: miker Date: Mon, 13 Sep 2010 15:17:44 +0000 Subject: [PATCH] teach fielder to cache (off by default), with a default timeout of 5min git-svn-id: svn://svn.open-ils.org/ILS/trunk@17629 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../src/perlmods/OpenILS/Application/Fielder.pm | 49 ++++++++++++++++++---- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Fielder.pm b/Open-ILS/src/perlmods/OpenILS/Application/Fielder.pm index 4703add92a..38a56c55b5 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Fielder.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Fielder.pm @@ -9,6 +9,7 @@ use OpenSRF::EX qw/:try/; use OpenSRF::AppSession; use OpenSRF::Utils::SettingsClient; +use OpenSRF::Utils::Cache; use OpenSRF::Utils::Logger qw/:level/; use OpenILS::Utils::Fieldmapper; @@ -16,6 +17,8 @@ use OpenSRF::Utils::JSON; use OpenILS::Utils::CStoreEditor qw/:funcs/; +use Digest::MD5 qw(md5_hex); + use XML::LibXML; use XML::LibXML::XPathContext; use XML::LibXSLT; @@ -31,6 +34,8 @@ our %namespace_map = ( my $log = 'OpenSRF::Utils::Logger'; +my $cache; +my $cache_timeout; my $parser = XML::LibXML->new(); my $xslt = XML::LibXSLT->new(); @@ -48,10 +53,15 @@ sub initialize { $log->debug( 'IDL XML file loaded' ); + $cache_timeout = $conf->config_value( + "apps", "open-ils.fielder", "app_settings", "cache_timeout" ) || 300; + generate_methods(); } -sub child_init {} +sub child_init { + $cache = OpenSRF::Utils::Cache->new('global'); +} sub fielder_fetch { my $self = shift; @@ -59,9 +69,12 @@ sub fielder_fetch { my $obj = shift; my $query = $obj->{query}; + my $nocache = $obj->{cache} ? 0 : 1; my $fields = $obj->{fields}; my $distinct = $obj->{distinct} ? 1 : 0; + return undef unless $query; + my $obj_class = $self->{class_hint}; my $fm_class = $self->{class_name}; @@ -69,18 +82,34 @@ sub fielder_fetch { $fields = [ $fm_class->real_fields ]; } - $log->debug( 'Field list: '. OpenSRF::Utils::JSON->perl2JSON( $fields ) ); - $log->debug( 'Query: '. OpenSRF::Utils::JSON->perl2JSON( $query ) ); - - return undef unless $fields; - return undef unless $query; - $fields = [$fields] if (!ref($fields)); + my $qstring = OpenSRF::Utils::JSON->perl2JSON( $query ); + my $fstring = OpenSRF::Utils::JSON->perl2JSON( [ sort { $a cmp $b } @$fields ] ); $log->debug( 'Query Class: '. $obj_class ); + $log->debug( 'Field list: '. $fstring ); + $log->debug( 'Query: '. $qstring ); + + my ($key,$res); + unless ($nocache) { + $key = 'open-ils.fielder_' . md5_hex( + $self->api_name . + $qstring . + $fstring . + $distinct . + $obj_class + ); + + $res = $cache->get_cache( $key ); + + if ($res) { + $client->respond($_) for (@$res); + return undef; + } + } - my $res = new_editor()->json_query({ + $res = new_editor()->json_query({ select => { $obj_class => $fields }, from => $obj_class, where => $query, @@ -91,8 +120,10 @@ sub fielder_fetch { $client->respond($value); } - return undef; + $client->respond_complete(); + $cache->put_cache( $key => $res => $cache_timeout ) unless ($nocache); + return undef; } sub generate_methods { -- 2.11.0