2 |
|
|
3 |
use warnings; |
use warnings; |
4 |
use strict; |
use strict; |
5 |
|
use base 'WebPAC::Common'; |
6 |
use Data::Dumper; |
use Data::Dumper; |
|
use Storable; |
|
7 |
|
|
8 |
=head1 NAME |
=head1 NAME |
9 |
|
|
79 |
return length($_); |
return length($_); |
80 |
}, ... |
}, ... |
81 |
}, |
}, |
82 |
cache_data_structure => './cache/ds/', |
db => $db_obj, |
83 |
lookup_regex => $lookup->regex, |
lookup_regex => $lookup->regex, |
84 |
); |
); |
85 |
|
|
86 |
Parametar C<filter> defines user supplied snippets of perl code which can |
Parametar C<filter> defines user supplied snippets of perl code which can |
87 |
be use with C<filter{...}> notation. |
be use with C<filter{...}> notation. |
88 |
|
|
|
Optional parameter C<cache_data_structure> defines path to directory |
|
|
in which cache file for C<data_structure> call will be created. |
|
|
|
|
89 |
Recommended parametar C<lookup_regex> is used to enable parsing of lookups |
Recommended parametar C<lookup_regex> is used to enable parsing of lookups |
90 |
in structures. |
in structures. |
91 |
|
|
96 |
my $self = {@_}; |
my $self = {@_}; |
97 |
bless($self, $class); |
bless($self, $class); |
98 |
|
|
|
$self->setup_cache_dir( $self->{'cache_data_structure'} ); |
|
|
|
|
99 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
100 |
} |
} |
101 |
|
|
|
=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'}; |
|
|
} |
|
|
} |
|
|
|
|
102 |
|
|
103 |
=head2 data_structure |
=head2 data_structure |
104 |
|
|
127 |
|
|
128 |
my $cache_file; |
my $cache_file; |
129 |
|
|
130 |
if (my $cache_path = $self->{'cache_data_structure'}) { |
if ($self->{'db'}) { |
131 |
my $id = $rec->{'000'}; |
my @ds = $self->{'db'}->load_ds($rec); |
132 |
$id = $rec->{'000'}->[0] if ($id =~ m/^ARRAY/o); |
$log->debug("load_ds( rec = ", sub { Dumper($rec) }, ") = ", sub { Dumper(@ds) }); |
133 |
unless (defined($id)) { |
return @ds if ($#ds > 0); |
134 |
$log->warn("Can't use cache_data_structure on records without unique identifier in field 000"); |
$log->debug("cache miss, creating"); |
|
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'}; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
135 |
} |
} |
136 |
|
|
137 |
undef $self->{'currnet_filename'}; |
undef $self->{'currnet_filename'}; |
239 |
|
|
240 |
} |
} |
241 |
|
|
242 |
if ($cache_file) { |
$self->{'db'}->save_ds( |
243 |
store { |
ds => \@ds, |
244 |
ds => \@ds, |
current_filename => $self->{'current_filename'}, |
245 |
current_filename => $self->{'current_filename'}, |
headline => $self->{'headline'}, |
246 |
headline => $self->{'headline'}, |
) if ($self->{'db'}); |
247 |
}, $cache_file; |
|
248 |
$log->debug("created storable cache file $cache_file"); |
$log->debug("ds: ", sub { Dumper(@ds) }); |
|
} |
|
249 |
|
|
250 |
return @ds; |
return @ds; |
251 |
|
|