=head1 VERSION
-Version 0.05
+Version 0.06
=cut
-our $VERSION = '0.05';
+our $VERSION = '0.06';
=head1 SYNOPSIS
If called with C<read_only> it will not disable caching if
called without write permission (but will die on C<save_ds>).
+Mandatory parametar C<database> is used as subdirectory in database directory.
+
=cut
sub new {
my $self = {@_};
bless($self, $class);
+ my $log = $self->_get_logger();
+
+ foreach my $p (qw/path database/) {
+ $log->logconfess("need $p") unless ($self->{$p});
+ }
+
$self->path( $self->{'path'} );
$self ? return $self : return undef;
Retrive from disk one data_structure records usually using field 000 as key
- my $ds = $db->load_ds( id => 42, database => 'name' );
+ my $ds = $db->load_ds( id => 42, prefix => 'name' );
This function will also perform basic sanity checking on returned
data and disable caching if data is corrupted (or changed since last
update).
+C<prefix> is used to differenciate different source input databases
+which are indexed in same database.
+
Returns hash or undef if cacheing is disabled or unavailable.
=cut
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+$/);
+ $log->logconfess("got hash, but id [$id] isn't number") unless ($id =~ /^\d+$/);
- my $database = $args->{database} || $self->{database};
+ my $database = $self->{database};
+ my $prefix = $args->{prefix} || '';
$log->logconfess("can't find database name") unless ($database);
- my $cache_file = "$cache_path/$database#$id";
- $cache_file =~ s#//#/#g;
+ my $cache_file = "$cache_path/$prefix/$database#$id";
+ $cache_file =~ s#//#/#go;
+
+open(my $fh, '>>', '/tmp/foo');
+print $fh "$cache_file\n";
+close($fh);
$log->debug("using cache_file $cache_file");
$db->save_ds(
id => $ds->{000}->[0],
- database => 'name',
+ prefix => 'name',
ds => $ds,
);
$log->logconfess("need $f") unless ($arg->{$f});
}
- my $database = $arg->{database} || $self->{database};
+ my $database = $self->{database};
$log->logconfess("can't find database name") unless ($database);
- my $cache_file = $self->{path} . '/' . $database . '#' . $arg->{id};
+ my $prefix = $arg->{prefix} || '';
+
+ my $cache_file = $self->{path} . "/$prefix/$database#" . $arg->{id};
+ $cache_file =~ s#//#/#go;
$log->debug("creating storable cache file $cache_file");
#!/usr/bin/perl -w
-use Test::More tests => 29;
+use Test::More tests => 31;
use Test::Exception;
use Cwd qw/abs_path/;
use blib;
diag "NULL Store";
-ok($db = new WebPAC::Store( debug => $debug ), "new");
+throws_ok { new WebPAC::Store() } qr/path/, 'new without path';
+
+throws_ok { new WebPAC::Store( path => '/tmp' ) } qr/database/, 'new without database';
+
+ok($db = new WebPAC::Store( path => '/tmp', database => '.', debug => $debug ), "new");
ok(! $db->path, "path");
ok(! $db->load_ds( id => 42 ), "load_ds non-existing");
-ok($db = new WebPAC::Store( path => $path, debug => $debug, no_log => $no_log ), "new without database");
+ok($db = new WebPAC::Store( path => $path, database => '.', debug => $debug, no_log => $no_log ), "new");
-throws_ok { $ds2 = $db->load_ds( id => 1 ) } qr/database/, "load_ds without database";
+ok(! $db->load_ds( id => 1, prefix => 'foobar' ), "load_ds with invalid prefix");
ok($ds2 = $db->load_ds( id => 1, database => '.' ), "load_ds");