3 |
use warnings; |
use warnings; |
4 |
use strict; |
use strict; |
5 |
use Data::Dumper; |
use Data::Dumper; |
|
use Storable; |
|
6 |
|
|
7 |
=head1 NAME |
=head1 NAME |
8 |
|
|
78 |
return length($_); |
return length($_); |
79 |
}, ... |
}, ... |
80 |
}, |
}, |
81 |
cache_data_structure => './cache/ds/', |
db => $webpac_db_obj, |
82 |
lookup_regex => $lookup->regex, |
lookup_regex => $lookup->regex, |
83 |
); |
); |
84 |
|
|
85 |
Parametar C<filter> defines user supplied snippets of perl code which can |
Parametar C<filter> defines user supplied snippets of perl code which can |
86 |
be use with C<filter{...}> notation. |
be use with C<filter{...}> notation. |
87 |
|
|
|
Optional parameter C<cache_data_structure> defines path to directory |
|
|
in which cache file for C<data_structure> call will be created. |
|
|
|
|
88 |
Recommended parametar C<lookup_regex> is used to enable parsing of lookups |
Recommended parametar C<lookup_regex> is used to enable parsing of lookups |
89 |
in structures. |
in structures. |
90 |
|
|
95 |
my $self = {@_}; |
my $self = {@_}; |
96 |
bless($self, $class); |
bless($self, $class); |
97 |
|
|
|
$self->setup_cache_dir( $self->{'cache_data_structure'} ); |
|
|
|
|
98 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
99 |
} |
} |
100 |
|
|
|
=head2 setup_cache_dir |
|
|
|
|
|
Check if specified cache directory exist, and if not, disable caching. |
|
|
|
|
|
$setup_cache_dir('./cache/ds/'); |
|
|
|
|
|
If you pass false or zero value to this function, it will disable |
|
|
cacheing. |
|
|
|
|
|
=cut |
|
|
|
|
|
sub setup_cache_dir { |
|
|
my $self = shift; |
|
|
|
|
|
my $dir = shift; |
|
|
|
|
|
my $log = $self->_get_logger(); |
|
|
|
|
|
if ($dir) { |
|
|
my $msg; |
|
|
if (! -e $dir) { |
|
|
$msg = "doesn't exist"; |
|
|
} elsif (! -d $dir) { |
|
|
$msg = "is not directory"; |
|
|
} elsif (! -w $dir) { |
|
|
$msg = "not writable"; |
|
|
} |
|
|
|
|
|
if ($msg) { |
|
|
undef $self->{'cache_data_structure'}; |
|
|
$log->warn("cache_data_structure $dir $msg, disabling..."); |
|
|
} else { |
|
|
$log->debug("using cache dir $dir"); |
|
|
} |
|
|
} else { |
|
|
$log->debug("disabling cache"); |
|
|
undef $self->{'cache_data_structure'}; |
|
|
} |
|
|
} |
|
|
|
|
101 |
|
|
102 |
=head2 data_structure |
=head2 data_structure |
103 |
|
|
126 |
|
|
127 |
my $cache_file; |
my $cache_file; |
128 |
|
|
129 |
if (my $cache_path = $self->{'cache_data_structure'}) { |
if ($self->{'db'}) { |
130 |
my $id = $rec->{'000'}; |
my @ds = $self->{'db'}->get_ds($rec); |
131 |
$id = $rec->{'000'}->[0] if ($id =~ m/^ARRAY/o); |
return @ds if (@ds); |
|
unless (defined($id)) { |
|
|
$log->warn("Can't use cache_data_structure on records without unique identifier in field 000"); |
|
|
undef $self->{'cache_data_structure'}; |
|
|
} else { |
|
|
$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_data_structure $cache_path corrupt. Use rm $cache_path/* to re-create it on next run!"); |
|
|
undef $self->{'cache_data_structure'}; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
132 |
} |
} |
133 |
|
|
134 |
undef $self->{'currnet_filename'}; |
undef $self->{'currnet_filename'}; |
236 |
|
|
237 |
} |
} |
238 |
|
|
239 |
if ($cache_file) { |
$self->{'db'}->put_gs( |
240 |
store { |
ds => \@ds, |
241 |
ds => \@ds, |
current_filename => $self->{'current_filename'}, |
242 |
current_filename => $self->{'current_filename'}, |
headline => $self->{'headline'}, |
243 |
headline => $self->{'headline'}, |
) if ($self->{'db'}); |
|
}, $cache_file; |
|
|
$log->debug("created storable cache file $cache_file"); |
|
|
} |
|
244 |
|
|
245 |
return @ds; |
return @ds; |
246 |
|
|