my $cache;
my $cache_timeout;
+
sub initialize {
$cache = OpenSRF::Utils::Cache->new('global');
my $sclient = OpenSRF::Utils::SettingsClient->new();
$searchhash->{offset} = 0;
$searchhash->{limit} = 0;
- my $ckey = $pfx . md5_hex($method . JSON->perl2JSON($searchhash));
+ my @search;
+ push( @search, ($_ => $$searchhash{$_})) for (sort keys %$searchhash);
+ my $ckey = $pfx . md5_hex($method . JSON->perl2JSON(\@search));
$searchhash->{offset} = $o;
$searchhash->{limit} = $l;
} else {
- $logger->debug("cache returned results: " . JSON->perl2JSON($result));
$docache = 0;
}
if( $docache ) {
$logger->debug("putting search cache $ckey\n");
- $cache->put_cache($ckey, \@recs, $cache_timeout);
+ put_cache($ckey, \@recs);
}
return { ids => \@recs,
my $offset = shift;
my $limit = shift;
- $logger->debug("fetching search cache $key\n");
+ $logger->debug("searching cache for $key\n");
+
+ my $start = $offset;
+ my $end = $offset + $limit - 1;
my $data = $cache->get_cache($key);
- return undef unless $data;
- $logger->debug("search_cache found some data: o=$offset, l=$limit");
+ return undef unless $data and ref $data eq 'ARRAY' and $$data[$start] and $$data[$end];
+
+ $logger->debug("search_cache found data for indices $start..$end");
+
+ my $result = [ (@$data[$start..$end]) ];
- #$data = JSON->JSON2perl($data);
- return undef unless $data and ref $data eq 'ARRAY';
- return undef unless $$data[$offset] and $$data[$offset+($limit-1)];
+ $logger->debug("cache returning results: [".
+ JSON->perl2JSON($$result[0]) . '...' .
+ JSON->perl2JSON($$result[$limit-1]) .']' );
- $logger->debug("search_cache found data..$offset - " . ($offset + ($limit - 1)));
- return [ @$data[$offset..($offset+$limit)] ];
+ return $result;
+}
+
+
+sub put_cache {
+ my( $key, $data ) = @_;
+ $logger->debug("search_cache putting ".
+ scalar(@$data)." items at key $key with timeout $cache_timeout");
+ $cache->put_cache($key, $data, $cache_timeout);
}
$limit ||= 10;
$offset ||= 0;
- my $ckey = $pfx . md5_hex($method . JSON->perl2JSON($args));
+ my @search;
+ push( @search, ($_ => $$args{$_}) ) for (sort keys %$args);
+ my $ckey = $pfx . md5_hex($method . JSON->perl2JSON(\@search));
+
my $recs = search_cache($ckey, $offset, $limit);
if(!$recs) {
$recs = new_editor()->request($method, %$args);
- $cache->put_cache($ckey, $recs, $cache_timeout);
+ put_cache($ckey, $recs);
$recs = [ @$recs[$offset..($offset + ($limit - 1))] ];
}