File Coverage

blib/lib/Data/Tubes/Util/Cache.pm
Criterion Covered Total %
statement 52 68 76.4
branch 13 22 59.0
condition n/a
subroutine 13 14 92.8
pod 3 3 100.0
total 81 107 75.7


line stmt bran cond sub pod time code
1             package Data::Tubes::Util::Cache;
2 3     3   1314 use strict;
  3         6  
  3         112  
3 3     3   18 use warnings;
  3         7  
  3         92  
4 3     3   16 use English qw< -no_match_vars >;
  3         5  
  3         20  
5 3     3   1205 use 5.010;
  3         10  
6             our $VERSION = '0.738';
7 3     3   21 use File::Path qw< mkpath >;
  3         4  
  3         194  
8              
9 3     3   1451 use File::Spec::Functions qw< splitpath catpath >;
  3         2402  
  3         201  
10 3     3   2105 use Storable qw< nstore retrieve >;
  3         6504  
  3         220  
11 3     3   25 use Log::Log4perl::Tiny qw< :easy :dead_if_first >;
  3         7  
  3         32  
12 3     3   2076 use Mo qw< default >;
  3         1108  
  3         18  
13             has repository => (default => sub { return {} });
14             has __filenames => (default => sub { return undef });
15             has max_items => (default => 0);
16              
17             sub _path {
18 5     5   10 my ($dir, $filename) = @_;
19 5         13 my ($v, $d) = splitpath($dir, 'no-file');
20 5         38 return catpath($v, $d, $filename);
21             }
22              
23             sub get {
24 18     18 1 46 my ($self, $key) = @_;
25 18         75 my $repo = $self->repository();
26 18 100       199 if (ref($repo) eq 'HASH') {
27 15 100       70 return unless exists $repo->{$key};
28 4         15 return $repo->{$key};
29             }
30 3         7 my $path = _path($repo, $key);
31 3 100       112 return retrieve($path) if -r $path;
32 2         9 return;
33             } ## end sub get
34              
35             sub _filenames {
36 0     0   0 my $self = shift;
37 0 0       0 if (my $retval = $self->__filenames()) {
38 0         0 return $retval;
39             }
40 0         0 my $repo = $self->repository();
41 0         0 my ($v, $d) = splitpath($repo, 'no-file');
42 0 0       0 opendir my $dh, $repo or return;
43 0         0 my @filenames = map { catpath($v, $d, $_) } readdir $dh;
  0         0  
44 0         0 closedir $dh;
45 0         0 $self->__filenames(\@filenames);
46 0         0 return \@filenames;
47             }
48              
49             sub purge {
50 6     6 1 16 my $self = shift;
51 6 50       19 my $max = $self->max_items() or return;
52 6         55 my $repo = $self->repository();
53              
54 6 50       45 if (ref($repo) eq 'HASH') {
55 6         19 my $n = scalar keys %$repo;
56 6         29 delete $repo->{(keys %$repo)[0]} while $n-- > $max;
57 6         14 return;
58             }
59              
60 0 0       0 my $filenames = $self->_filenames() or return;
61 0         0 while (@$filenames > $max) {
62 0         0 my $filename = shift @$filenames;
63 0         0 unlink $filename;
64             }
65 0         0 return;
66             } ## end sub purge
67              
68             sub set {
69 13     13 1 28 my ($self, $key, $data) = @_;
70 13         53 my $repo = $self->repository();
71 13 100       124 return $repo->{$key} = $data if ref($repo) eq 'HASH';
72 2 50       4 eval {
73 2 100       9 mkpath($repo) unless -d $repo;
74 2         462 nstore($data, _path($repo, $key));
75 2         453 1;
76             } or LOGWARN $EVAL_ERROR;
77 2         7 return $data;
78             }