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   1366 use strict;
  3         6  
  3         111  
3 3     3   18 use warnings;
  3         6  
  3         96  
4 3     3   15 use English qw< -no_match_vars >;
  3         6  
  3         20  
5 3     3   1243 use 5.010;
  3         11  
6             our $VERSION = '0.737';
7 3     3   21 use File::Path qw< mkpath >;
  3         6  
  3         198  
8              
9 3     3   1480 use File::Spec::Functions qw< splitpath catpath >;
  3         2561  
  3         214  
10 3     3   1353 use Storable qw< nstore retrieve >;
  3         6523  
  3         192  
11 3     3   23 use Log::Log4perl::Tiny qw< :easy :dead_if_first >;
  3         6  
  3         28  
12 3     3   2121 use Mo qw< default >;
  3         1110  
  3         17  
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   12 my ($dir, $filename) = @_;
19 5         16 my ($v, $d) = splitpath($dir, 'no-file');
20 5         80 return catpath($v, $d, $filename);
21             }
22              
23             sub get {
24 18     18 1 38 my ($self, $key) = @_;
25 18         53 my $repo = $self->repository();
26 18 100       183 if (ref($repo) eq 'HASH') {
27 15 100       49 return unless exists $repo->{$key};
28 4         12 return $repo->{$key};
29             }
30 3         11 my $path = _path($repo, $key);
31 3 100       155 return retrieve($path) if -r $path;
32 2         11 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 11 my $self = shift;
51 6 50       12 my $max = $self->max_items() or return;
52 6         39 my $repo = $self->repository();
53              
54 6 50       35 if (ref($repo) eq 'HASH') {
55 6         13 my $n = scalar keys %$repo;
56 6         21 delete $repo->{(keys %$repo)[0]} while $n-- > $max;
57 6         13 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 26 my ($self, $key, $data) = @_;
70 13         29 my $repo = $self->repository();
71 13 100       110 return $repo->{$key} = $data if ref($repo) eq 'HASH';
72 2 50       4 eval {
73 2 100       18 mkpath($repo) unless -d $repo;
74 2         497 nstore($data, _path($repo, $key));
75 2         553 1;
76             } or LOGWARN $EVAL_ERROR;
77 2         7 return $data;
78             }