| 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
Condition100%3/3
defined $self->[0] and $self->[0] <= &Time::HiRes::time()
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()
| |||||||||||||||||||||||||||||
| 28 | 9 | undef $self->[VALUE]; | |||||||||||||||||||||||||||
| 29 | 9 | undef $self->[TIME]; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 32 | ▶ | wantarray ? ($self->[VALUE], $self->[TIME]) : $self->[VALUE]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 38 | 6 | ▶ | return wantarray ? ($self->[VALUE], $self->[TIME]) : $self->[VALUE]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
not $expiry or $expiry <= 0
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||
| 73 | 18 | $obj->get_or_set($code); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 3 | 1; | |||||||||||||||||||||||||||
| 78 | __END__ | ||||||||||||||||||||||||||||