6 |
use base 'WebPAC::Common'; |
use base 'WebPAC::Common'; |
7 |
use Storable; |
use Storable; |
8 |
use File::Path; |
use File::Path; |
9 |
use Data::Dumper; |
use Data::Dump qw/dump/; |
10 |
|
|
11 |
=head1 NAME |
=head1 NAME |
12 |
|
|
14 |
|
|
15 |
=head1 VERSION |
=head1 VERSION |
16 |
|
|
17 |
Version 0.10 |
Version 0.11 |
18 |
|
|
19 |
=cut |
=cut |
20 |
|
|
21 |
our $VERSION = '0.10'; |
our $VERSION = '0.11'; |
22 |
|
|
23 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
24 |
|
|
59 |
If called with C<read_only> it will not disable caching if |
If called with C<read_only> it will not disable caching if |
60 |
called without write permission (but will die on C<save_ds>). |
called without write permission (but will die on C<save_ds>). |
61 |
|
|
62 |
Mandatory parametar C<database> is used as subdirectory in database directory. |
Optional parametar C<database> will be used used as subdirectory in path if no |
63 |
|
database in specified when calling other functions. |
64 |
|
|
65 |
=cut |
=cut |
66 |
|
|
89 |
If you pass false or zero value to this function, it will disable |
If you pass false or zero value to this function, it will disable |
90 |
cacheing. |
cacheing. |
91 |
|
|
92 |
You can also example C<< $db->{path} >> to get current cache path. |
You can also call this function to get current cache path. |
93 |
|
|
94 |
|
my $cache_path = $db->path; |
95 |
|
|
96 |
=cut |
=cut |
97 |
|
|
99 |
my $self = shift; |
my $self = shift; |
100 |
|
|
101 |
my $dir = shift; |
my $dir = shift; |
102 |
|
|
103 |
|
return $self->{path} unless defined($dir); |
104 |
|
|
105 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
106 |
|
|
136 |
|
|
137 |
Retrive from disk one data_structure records usually using field 000 as key |
Retrive from disk one data_structure records usually using field 000 as key |
138 |
|
|
139 |
my $ds = $db->load_ds( id => 42, prefix => 'name', database => 'ps' ); |
my $ds = $db->load_ds( |
140 |
|
database => 'ps', |
141 |
|
input => 'name', |
142 |
|
id => 42, |
143 |
|
); |
144 |
|
|
145 |
This function will also perform basic sanity checking on returned |
This function will also perform basic sanity checking on returned |
146 |
data and disable caching if data is corrupted (or changed since last |
data and disable caching if data is corrupted (or changed since last |
147 |
update). |
update). |
148 |
|
|
149 |
C<prefix> is used to differenciate different source input databases |
C<input> is used to differenciate different source input databases |
150 |
which are indexed in same database. |
which are indexed in same database. |
151 |
|
|
152 |
C<database> if B<optional> argument which will override database name used when creating |
C<database> if B<optional> argument which will override database name used when creating |
177 |
$log->logconfess("got hash, but id [$id] isn't number") unless ($id =~ /^\d+$/); |
$log->logconfess("got hash, but id [$id] isn't number") unless ($id =~ /^\d+$/); |
178 |
|
|
179 |
my $database = $args->{database} || $self->{database}; |
my $database = $args->{database} || $self->{database}; |
180 |
my $prefix = $args->{prefix} || ''; |
my $input = $args->{input} || ''; |
181 |
|
|
182 |
$log->logconfess("can't find database name") unless ($database); |
$log->logconfess("can't find database name") unless ($database); |
183 |
|
|
184 |
my $cache_file = "$cache_path/$database/$prefix/$id"; |
my $cache_file = "$cache_path/$database/$input/$id"; |
185 |
$cache_file =~ s#//#/#go; |
$cache_file =~ s#//#/#go; |
186 |
|
|
187 |
$log->debug("using cache_file $cache_file"); |
$log->debug("using cache_file $cache_file"); |
210 |
Store data_structure on disk. |
Store data_structure on disk. |
211 |
|
|
212 |
$db->save_ds( |
$db->save_ds( |
213 |
|
database => 'name', |
214 |
|
input => 'name', |
215 |
id => $ds->{000}->[0], |
id => $ds->{000}->[0], |
|
prefix => 'name', |
|
216 |
ds => $ds, |
ds => $ds, |
217 |
); |
); |
218 |
|
|
229 |
|
|
230 |
return unless($self->{'path'}); |
return unless($self->{'path'}); |
231 |
|
|
232 |
my $arg = {@_}; |
my $args = {@_}; |
233 |
|
|
234 |
my $log = $self->_get_logger; |
my $log = $self->_get_logger; |
235 |
|
$log->debug("save_ds arguments:", dump( \@_ )); |
236 |
|
|
237 |
foreach my $f (qw/id ds/) { |
foreach my $f (qw/id ds/) { |
238 |
$log->logconfess("need $f") unless ($arg->{$f}); |
$log->logconfess("need $f") unless (defined($args->{$f})); |
239 |
} |
} |
240 |
|
|
241 |
my $database = $self->{database}; |
my $database = $args->{database} || $self->{database}; |
242 |
$log->logconfess("can't find database name") unless ($database); |
$log->logconfess("can't find database name") unless ($database); |
243 |
|
|
244 |
my $prefix = $arg->{prefix} || ''; |
my $input = $args->{input} || ''; |
245 |
|
|
246 |
my $cache_file = $self->{path} . '/' . $prefix . '/'; |
my $cache_file = $self->{path} . "/$database/$input/"; |
247 |
$cache_file =~ s#//#/#go; |
$cache_file =~ s#//#/#go; |
248 |
|
|
249 |
mkpath($cache_file) unless (-d $cache_file); |
mkpath($cache_file) unless (-d $cache_file); |
250 |
|
|
251 |
$cache_file .= $arg->{id}; |
$cache_file .= $args->{id}; |
252 |
|
|
253 |
$log->debug("creating storable cache file $cache_file"); |
$log->debug("creating storable cache file $cache_file"); |
254 |
|
|
255 |
return store { |
return store { |
256 |
ds => $arg->{ds}, |
ds => $args->{ds}, |
257 |
id => $arg->{id}, |
id => $args->{id}, |
258 |
}, $cache_file; |
}, $cache_file; |
259 |
|
|
260 |
} |
} |
261 |
|
|
262 |
=head2 save_lookup |
=head2 save_lookup |
263 |
|
|
264 |
$db->save_lookup( $database, $input, $key, $lookup ); |
$db->save_lookup( |
265 |
|
database => $database, |
266 |
|
input => $input, |
267 |
|
key => $key, |
268 |
|
data => $lookup, |
269 |
|
); |
270 |
|
|
271 |
=cut |
=cut |
272 |
|
|
273 |
sub save_lookup { |
sub save_lookup { |
274 |
my $self = shift; |
my $self = shift; |
275 |
my ($database, $input, $key, $lookup) = @_; |
my $args = {@_}; |
276 |
|
|
277 |
my $log = $self->_get_logger; |
my $log = $self->_get_logger; |
278 |
|
|
279 |
my $path = $self->{'path'} . "/lookup/$input"; |
foreach my $r (qw/input key data/) { |
280 |
|
$log->logconfess("need '$r'") unless defined($args->{$r}); |
281 |
|
} |
282 |
|
|
283 |
|
my $database = $args->{database} || $self->{database} || $log->logconfess("no database?"); |
284 |
|
|
285 |
|
my $path = $self->{path} . "/lookup/$database/" . $args->{input}; |
286 |
|
|
287 |
mkpath($path) unless (-d $path); |
mkpath($path) unless (-d $path); |
288 |
|
|
289 |
$path .= "/$key"; |
$path .= "/" . $args->{key}; |
290 |
|
|
291 |
if (store $lookup, $path) { |
if (store $args->{data}, $path) { |
292 |
$log->info("saved lookup $path"); |
$log->info("saved lookup $path"); |
293 |
|
return 1; |
294 |
} else { |
} else { |
295 |
$log->logwarn("can't store lookup $database/$input/$key in $path: $!"); |
$log->logwarn("can't store lookup $database/", $args->{input}, "/", $args->{key}, " in $path: $!"); |
296 |
|
return undef; |
297 |
} |
} |
|
|
|
|
|
|
298 |
} |
} |
299 |
|
|
300 |
|
|