blib/lib/Cache/Scalar/WithExpiry.pm

filter: Statementstmt 100% 52 / 52 Branchbran 91.6% 11 / 12 Conditioncond 77.7% 7 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 13 / 13 Podpod 100% 6 / 6 totaltotal 93.8% 92 / 98 CC 10 SCAR 9.0
CC
10
Cov
96%
get
4.0
set
4.0
get_or_set
3.0
CRAP 2.5
↑ Summary
1 package Cache::Scalar::WithExpiry;
2 3 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = "0.01";
7  
8 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 our @EXPORT = qw/cache_with_expiry/;
13  
14 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 TIME => 0,
16 VALUE => 1,
17 3 };
18  
19 12 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
20 12 my ($class) = @_;
21 12 bless [undef, undef], $class;
22 }
23  
24 32 ▶sub get {
Subroutine get: called CC=4 SCAR=13.9
Pod documented
25 32 my ($self) = @_;
26  
27 32 ▶ if (defined $self->[TIME] && $self->[TIME] <= Time::HiRes::time()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[0] and $self->[0] <= &Time::HiRes::time()) 9 23 32
Condition100%3/3
defined $self->[0] and $self->[0] <= &Time::HiRes::time()
!ll&&!rl&&r
13109
MC/DC100%2/2
defined $self->[0] and $self->[0] <= &Time::HiRes::time()
defined $self->[0] $self->[0] <= &Time::HiRes::time()
Truth table100%3/3
A and B
A= defined $self->[0]B= $self->[0] <= &Time::HiRes::time()
ABresult
0X0
100
111
28 9 undef $self->[VALUE];
29 9 undef $self->[TIME];
30 }
31 32 ▶ wantarray ? ($self->[VALUE], $self->[TIME]) : $self->[VALUE];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 32 32
32 }
33  
34 20 ▶sub get_or_set {
Subroutine get_or_set: called CC=3 SCAR=11.0
Pod documented
35 20 my ($self, $code) = @_;
36  
37 20 ▶ if (defined $self->get) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->get) { } 6 14 20
38 6 ▶ return wantarray ? ($self->[VALUE], $self->[TIME]) : $self->[VALUE];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 3 6
39 }
40 else {
41 14 my ($val, $expiry) = $code->();
42 14 return $self->set($val, $expiry);
43 }
44 }
45  
46 18 ▶sub set {
Subroutine set: called CC=4 SCAR=13.9
Pod documented
47 18 my ($self, $val, $expiry) = @_;
48  
49 18 ▶ if (!$expiry || $expiry <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $expiry or $expiry <= 0) { } 2 16 18
Condition66%2/3
not $expiry or $expiry <= 0
l!l&&r!l&&!r
2016
MC/DC50%1/2
not $expiry or $expiry <= 0
not $expiry $expiry <= 0
Truth table66%2/3
not(A) or B
A= not $expiryB= $expiry <= 0
ABresult
1X1
011
000
50 2 Carp::carp 'Expiry time is required. Value is not to be cached.';
51 2 undef $expiry;
52 2 undef $self->[VALUE];
53 2 undef $self->[TIME];
54 }
55 else {
56 16 $self->[TIME] = $expiry;
57 16 $self->[VALUE] = $val;
58 }
59 18 ▶ wantarray ? ($val, $expiry) : $val;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 15 18
60 }
61  
62 1 ▶sub delete :method {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
63 1 my ($self) = @_;
64 1 undef $self->[VALUE];
65 1 return undef;
66 }
67  
68 {
69 3 my %_obj;
70 18 ▶ sub cache_with_expiry(&) {
Subroutine cache_with_expiry: called CC=2 SCAR=7.0
Pod documented
71 18 my $code = shift;
72 18 ▶ my $obj = $_obj{$code+0} ||= __PACKAGE__->new;
Condition66%2/3
$_obj{$code + 0} ||= __PACKAGE__->new
l!l&&r!l&&!r
1260
MC/DC0%0/2
$_obj{$code + 0} ||= __PACKAGE__->new
$_obj{$code + 0} __PACKAGE__->new
Truth table66%2/3
A ||= B
A= $_obj{$code + 0}B= __PACKAGE__->new
ABresult
1X1
011
000
73 18 $obj->get_or_set($code);
74 }
75 }
76  
77 3 1;
78 __END__
↑ Summary