| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$args{"LIFETIME"} ||= 0
$args{"LIFETIME"}
Truth table100%2/2
A
A= $args{"LIFETIME"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 3 | ▶ | $args{NUM_USES} ||= 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{"NUM_USES"} ||= 0
MC/DC100%1/1
$args{"NUM_USES"} ||= 0
$args{"NUM_USES"}
Truth table100%2/2
A
A= $args{"NUM_USES"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | ▶ | $args{C} = delete $args{HASH} || \%cache; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $args{'HASH'} || \%cache
MC/DC100%1/1
delete $args{'HASH'} || \%cache
delete $args{'HASH'}
Truth table100%2/2
A
A= delete $args{'HASH'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 14 | my ($self, $key, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 14 | ▶ | my $expire_time = $self->{LIFETIME} > 0 ? $self->{LIFETIME} + time : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 35 | ▶ | unless (exists $_[0]{C}{"V$_[1]"}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 7 | ▶ | $DEBUG and print STDERR " Not in underlying hash at all.\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | $_[0]{LIFETIME} and print STDERR " Time to live for this item: $ttl\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | $_[0]{NUM_USES} and print STDERR " Uses remaining: $num_uses_left\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 28 | ▶ | if ( (! $_[0]{LIFETIME} || $expire_time > time) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
!$_[0]{"LIFETIME"} || $expire_time > &Memoize::Expire::time() and !$_[0]{"NUM_USES"} || $num_uses_left > 0
not $_[0]{"LIFETIME"} or $expire_time > &Memoize::Expire::time()
not $_[0]{"NUM_USES"} or $num_uses_left > 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | && (! $_[0]{NUM_USES} || $num_uses_left > 0 )) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 23 | ▶ | $DEBUG and print STDERR " (Still good)\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 23 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 5 | ▶ | $DEBUG and print STDERR " (Expired)\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 4 | ▶ | return substr $key, 1 if 'V' eq substr $key, 0, 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||