Memoize/Expire.pm

filter: Statementstmt 81.4% 57 / 70 Branchbran 52.6% 20 / 38 Conditioncond 100% 15 / 15 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 77.4% 110 / 142 CC 26 SCAR 22.4
CC
26
Cov
75%
EXISTS
16.0
TIEHASH
14.4
FETCH
3.1
CRAP 9.4
« Memoize.pm ↑ Summary Memoize/Storable.pm »
1 2 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Memoize::Expire;
4 2 our $VERSION = '1.17';
5  
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 our $DEBUG;
8  
9 # The format of the metadata is:
10 # (4-byte number of last-access-time) (For LRU when I implement it)
11 # (4-byte expiration time: unsigned seconds-since-unix-epoch)
12 # (2-byte number-of-uses-before-expire)
13  
14 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.0
15 2 eval {require Time::HiRes};
16 2 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 2 0 2
17 2 Time::HiRes->import('time');
18 }
19 }
20  
21 3 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=7 SCAR=26.7
22 3 my ($package, %args) = @_;
23 3 my %cache;
24 3 ▶ if ($args{TIE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"TIE"}) 0 3 3
25 0 my ($module, @opts) = @{$args{TIE}};
26 0 my $modulefile = $module . '.pm';
27 0 $modulefile =~ s{::}{/}g;
28 0 eval { require $modulefile };
29 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
30 0 croak "Memoize::Expire: Couldn't load hash tie module `$module': $@; aborting";
31 }
32 0 my $rc = (tie %cache => $module, @opts);
33 0 ▶ unless ($rc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rc) 0 0 0
34 0 croak "Memoize::Expire: Couldn't tie hash to `$module': $@; aborting";
35 }
36 }
37 3 ▶ $args{LIFETIME} ||= 0;
Condition100%2/2
$args{"LIFETIME"} ||= 0
l!l
12
MC/DC100%1/1
$args{"LIFETIME"} ||= 0
$args{"LIFETIME"}
Truth table100%2/2
A
A= $args{"LIFETIME"}
Aresult
11
00
38 3 ▶ $args{NUM_USES} ||= 0;
Condition100%2/2
$args{"NUM_USES"} ||= 0
l!l
12
MC/DC100%1/1
$args{"NUM_USES"} ||= 0
$args{"NUM_USES"}
Truth table100%2/2
A
A= $args{"NUM_USES"}
Aresult
11
00
39 3 ▶ $args{C} = delete $args{HASH} || \%cache;
Condition100%2/2
delete $args{'HASH'} || \%cache
l!l
12
MC/DC100%1/1
delete $args{'HASH'} || \%cache
delete $args{'HASH'}
Truth table100%2/2
A
A= delete $args{'HASH'}
Aresult
11
00
40 3 bless \%args => $package;
41 }
42  
43 14 ▶sub STORE {
Subroutine STORE: called CC=3 SCAR=11.0
44 14 ▶ $DEBUG and print STDERR " >> Store $_[1] $_[2]\n";
Branch: 1 branch
BranchTrueFalseTotal
$Memoize::Expire::DEBUG and print STDERR " >> Store $_[1] $_[2]\n" 0 14 14
45 14 my ($self, $key, $value) = @_;
46 14 ▶ my $expire_time = $self->{LIFETIME} > 0 ? $self->{LIFETIME} + time : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'LIFETIME'} > 0 ? : 6 8 14
47 # The call that results in a value to store into the cache is the
48 # first of the NUM_USES allowed calls.
49 14 my $header = _make_header(time, $expire_time, $self->{NUM_USES}-1);
50 14 @{$self->{C}}{"H$key", "V$key"} = ($header, $value);
51 14 $value;
52 }
53  
54 27 ▶sub FETCH {
Subroutine FETCH: called CC=3 SCAR=11.3
55 27 ▶ $DEBUG and print STDERR " >> Fetch cached value for $_[1]\n";
Branch: 1 branch
BranchTrueFalseTotal
$Memoize::Expire::DEBUG and print STDERR " >> Fetch cached value for $_[1]\n" 0 27 27
56 27 my ($last_access, $expire_time, $num_uses_left) = _get_header($_[0]{C}{"H$_[1]"});
57 27 ▶ $DEBUG and print STDERR " >> (ttl: ", ($expire_time-time()), ", nuses: $num_uses_left)\n";
Branch: 1 branch
BranchTrueFalseTotal
$Memoize::Expire::DEBUG and print STDERR " >> (ttl: ", $expire_time - &Memoize::Expire::time(), ", nuses: $num_uses_left)\n" 0 27 27
58 27 $_[0]{C}{"H$_[1]"} = _make_header(time, $expire_time, --$num_uses_left);
59 27 $_[0]{C}{"V$_[1]"};
60 }
61  
62 35 ▶sub EXISTS {
Subroutine EXISTS: called CC=13 SCAR=27.7
63 35 ▶ $DEBUG and print STDERR " >> Exists $_[1]\n";
Branch: 1 branch
BranchTrueFalseTotal
$Memoize::Expire::DEBUG and print STDERR " >> Exists $_[1]\n" 0 35 35
64 35 ▶ unless (exists $_[0]{C}{"V$_[1]"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $_[0]{"C"}{"V$_[1]"}) 7 28 35
65 7 ▶ $DEBUG and print STDERR " Not in underlying hash at all.\n";
Branch: 1 branch
BranchTrueFalseTotal
$Memoize::Expire::DEBUG and print STDERR " Not in underlying hash at all.\n" 0 7 7
66 7 return 0;
67 }
68 28 my $item = $_[0]{C}{"H$_[1]"};
69 28 my ($last_access, $expire_time, $num_uses_left) = _get_header($item);
70 28 my $ttl = $expire_time - time;
71 28 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Memoize::Expire::DEBUG) 0 28 28
72 0 ▶ $_[0]{LIFETIME} and print STDERR " Time to live for this item: $ttl\n";
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"LIFETIME"} and print STDERR " Time to live for this item: $ttl\n" 0 0 0
73 0 ▶ $_[0]{NUM_USES} and print STDERR " Uses remaining: $num_uses_left\n";
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"NUM_USES"} and print STDERR " Uses remaining: $num_uses_left\n" 0 0 0
74 }
75 28 ▶ if ( (! $_[0]{LIFETIME} || $expire_time > time)
Branch: 1 branch
BranchTrueFalseTotal
if (!$_[0]{'LIFETIME'} || $expire_time > &Memoize::Expire::time() and !$_[0]{'NUM_USES'} || $num_uses_left > 0) { } 23 5 28
Condition100%9/9
!$_[0]{"LIFETIME"} || $expire_time > &Memoize::Expire::time() and !$_[0]{"NUM_USES"} || $num_uses_left > 0
!ll&&!rl&&r
3223
not $_[0]{"LIFETIME"} or $expire_time > &Memoize::Expire::time()
l!l&&r!l&&!r
7183
not $_[0]{"NUM_USES"} or $num_uses_left > 0
l!l&&r!l&&!r
1852
MC/DC75%3/4
!$_[0]{"LIFETIME"} || $expire_time > &Memoize::Expire::time() and !$_[0]{"NUM_USES"} || $num_uses_left > 0
not $_[0]{"LIFETIME"} $expire_time > &Memoize::Expire::time() not $_[0]{"NUM_USES"} $num_uses_left > 0
Truth table57%4/7
not(A) or B and not(C) or D
A= not $_[0]{"LIFETIME"}B= $expire_time > &Memoize::Expire::time()C= not $_[0]{"NUM_USES"}D= $num_uses_left > 0
ABCDresult
00XX0
1X000
01000
1X1X1
1X011
011X1
01011
76 && (! $_[0]{NUM_USES} || $num_uses_left > 0 )) {
77 23 ▶ $DEBUG and print STDERR " (Still good)\n";
Branch: 1 branch
BranchTrueFalseTotal
$Memoize::Expire::DEBUG and print STDERR " (Still good)\n" 0 23 23
78 23 return 1;
79 } else {
80 5 ▶ $DEBUG and print STDERR " (Expired)\n";
Branch: 1 branch
BranchTrueFalseTotal
$Memoize::Expire::DEBUG and print STDERR " (Expired)\n" 0 5 5
81 5 return 0;
82 }
83 }
84  
85 1 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
86 1 scalar keys %{$_[0]{C}};
87 1 &NEXTKEY;
88 }
89  
90 3 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=3 SCAR=11.0
91 3 ▶ while (defined(my $key = each %{$_[0]{C}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $key = each %{$_[0]{"C"};})) 4 1 5
92 4 ▶ return substr $key, 1 if 'V' eq substr $key, 0, 1;
Branch: 1 branch
BranchTrueFalseTotal
if "V" eq substr($key, 0, 1) 2 2 4
93 }
94 1 undef;
95 }
96  
97 # Arguments: last access time, expire time, number of uses remaining
98 41 ▶sub _make_header {
Subroutine _make_header: called CC=1 SCAR=0.0
99 41 pack "N N n", @_;
100 }
101  
102 # Return last access time, expire time, number of uses remaining
103 55 ▶sub _get_header {
Subroutine _get_header: called CC=1 SCAR=0.0
104 55 unpack "N N n", substr($_[0], 0, 10);
105 }
106  
107 2 1;
108  
109 __END__
« Memoize.pm ↑ Summary Memoize/Storable.pm »