--- trunk/lib/WebPAC/Store.pm 2005/12/05 17:47:45 216 +++ trunk/lib/WebPAC/Store.pm 2005/12/05 17:47:51 217 @@ -6,6 +6,7 @@ use base 'WebPAC::Common'; use Storable; use File::Path; +use Data::Dumper; =head1 NAME @@ -13,11 +14,11 @@ =head1 VERSION -Version 0.04 +Version 0.05 =cut -our $VERSION = '0.04'; +our $VERSION = '0.05'; =head1 SYNOPSIS @@ -45,6 +46,7 @@ my $db = new WebPAC::Store( path => '/path/to/cache/ds/', + database => 'name', read_only => 1, ); @@ -116,13 +118,9 @@ =head2 load_ds -Retrive from disk one data_structure records using field 000 as key +Retrive from disk one data_structure records usually using field 000 as key - my $ds = $db->load_ds( 42 ); - -There is also a more verbose form, similar to C - - my $ds = $db->load_ds( id => 42 ); + my $ds = $db->load_ds( id => 42, database => 'name' ); This function will also perform basic sanity checking on returned data and disable caching if data is corrupted (or changed since last @@ -135,47 +133,46 @@ sub load_ds { my $self = shift; - return unless $self->{'path'}; - my $log = $self->_get_logger; my $cache_path = $self->{'path'}; - my $id = shift; - if (lc($id) eq 'id') { - $id = shift; - $log->logconfess("got hash, but without key id") unless (defined($id)); - $log->logconfess("got hash, but id isn't number") unless ($id =~ /^\d+$/); + if (! $cache_path) { + $log->warn("path not set, ignoring load_ds"); + return; } - if (! defined($id)) { - $log->warn("called without id"); - return undef; - } else { - my $cache_file = "$cache_path/$id"; - if (-r $cache_file) { - my $ds_ref = retrieve($cache_file); - if ($ds_ref) { - $log->debug("cache hit: $cache_file"); - my $ok = 1; -# foreach my $f (qw(current_filename headline)) { -# if ($ds_ref->{$f}) { -# $self->{$f} = $ds_ref->{$f}; -# } else { -# $ok = 0; -# } -# }; - if ($ok && $ds_ref->{'ds'}) { - return $ds_ref->{'ds'}; - } else { - $log->warn("cache entry $cache_file corrupt. Use rm $cache_path/* to re-create it on next run!"); - undef $self->{'path'}; - } + $log->logconfess("arguments for load_ds must be HASH") unless (ref(\@_) eq 'ARRAY' && ($#_ % 2 == 1)); + + my $args = {@_}; + my $id = $args->{id}; + + $log->logconfess("got hash, but without id") unless (defined($id)); + $log->logconfess("got hash, but id isn't number") unless ($id =~ /^\d+$/); + + my $database = $args->{database} || $self->{database}; + + $log->logconfess("can't find database name") unless ($database); + + my $cache_file = "$cache_path/$database#$id"; + $cache_file =~ s#//#/#g; + + $log->debug("using cache_file $cache_file"); + + if (-r $cache_file) { + my $ds_ref = retrieve($cache_file); + if ($ds_ref) { + $log->debug("cache hit: $cache_file"); + if ($ds_ref->{'ds'}) { + return $ds_ref->{'ds'}; + } else { + $log->warn("cache entry $cache_file corrupt. Use rm $cache_path/* to re-create it on next run!"); + undef $self->{'path'}; } - } else { - #$log->warn("cache entry $cache_file doesn't exist"); - return undef; } + } else { + #$log->warn("cache entry $cache_file doesn't exist"); + return undef; } return undef; @@ -187,6 +184,7 @@ $db->save_ds( id => $ds->{000}->[0], + database => 'name', ds => $ds, ); @@ -211,7 +209,10 @@ $log->logconfess("need $f") unless ($arg->{$f}); } - my $cache_file = $self->{path} . '/' . $arg->{id}; + my $database = $arg->{database} || $self->{database}; + $log->logconfess("can't find database name") unless ($database); + + my $cache_file = $self->{path} . '/' . $database . '#' . $arg->{id}; $log->debug("creating storable cache file $cache_file");