| 1 | package InternetData::Cache; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 9 | our $VERSION = '1.17.0'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # A node is [key, value, expires_at, older, newer]. The hash finds a node, the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # links order them by recency, so a get, a set and an eviction are each O(1). A | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # recency ARRAY would be O(n) per touch, and once the cache is full that is every | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # single insert. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 9 | ▶ | use constant { KEY => 0, VALUE => 1, EXPIRES => 2, OLDER => 3, NEWER => 4 }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # The longest a spent trial's or a reached cap's answer is held, in seconds. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 9 | ▶ | use constant REDUCED_TTL => 60; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 437 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 437 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | return bless { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | max => $args{max}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | ttl => $args{ttl}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 437 | nodes => {}, | |||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | newest => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | oldest => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # undef is a miss. A hit is always a blessed result, so the two never collide. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 142 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 142 | my ($self, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 142 | my $node = $self->{nodes}{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 142 | ▶ | return undef unless $node; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 7 | ▶ | if ($node->[EXPIRES] <= Time::HiRes::time()) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | $self->_forget($node); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 6 | $self->_promote($node); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 6 | return $node->[VALUE]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 63 | ▶ | sub set { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=4 SCAR=15.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 63 | my ($self, $key, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 63 | ▶ | return unless $self->{max} > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 63 | my $expires = Time::HiRes::time() + $self->_life_of($value); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 63 | ▶ | if (my $node = $self->{nodes}{$key}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | @{$node}[VALUE, EXPIRES] = ($value, $expires); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | $self->_promote($node); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 63 | my $node = [$key, $value, $expires, undef, undef]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 63 | $self->{nodes}{$key} = $node; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 63 | $self->_link($node); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 63 | ▶ | $self->_forget($self->{oldest}) while keys %{$self->{nodes}} > $self->{max}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub size { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
size: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | return scalar keys %{ $_[0]{nodes} }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # When the answer held for $key goes stale, as Time::HiRes::time() reads, or undef. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 14 | ▶ | sub expiry { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
expiry: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 14 | my ($self, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 14 | my $node = $self->{nodes}{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 14 | ▶ | return $node ? $node->[EXPIRES] : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # A reduced answer, a spent trial's or a reached cap's, ends with a new UTC day | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | # or a raised cap, so it stays a minute at most rather than the TTL. It is still | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # cached, or a busy process would send every repeat of an address to the API. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 63 | ▶ | sub _life_of { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_life_of: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 63 | my ($self, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 63 | ▶ | my $reduced = Scalar::Util::blessed($value) && $value->can('trial_expired') | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%6/9
builtin::blessed($value) && $value->can('trial_expired') && ($value->trial_expired || $value->cap_reached)
builtin::blessed($value) and $value->can("trial_expired")
$value->trial_expired or $value->cap_reached
MC/DC50%2/4
builtin::blessed($value) && $value->can('trial_expired') && ($value->trial_expired || $value->cap_reached)
builtin::blessed($value)
$value->can("trial_expired")
$value->trial_expired
$value->cap_reached
Truth table60%3/5
A and B && C or D
A= builtin::blessed($value)B= $value->can("trial_expired")C= $value->trial_expiredD= $value->cap_reached
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | && ($value->trial_expired || $value->cap_reached); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 63 | ▶ | return $self->{ttl} unless $reduced; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 11 | ▶ | return $self->{ttl} < REDUCED_TTL ? $self->{ttl} : REDUCED_TTL; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 6 | ▶ | sub _promote { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_promote: called CC=3 SCAR=15.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 6 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 6 | ▶ | return if $self->{newest} && $self->{newest} == $node; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"newest"} and $self->{"newest"} == $node
MC/DC0%0/2
$self->{"newest"} and $self->{"newest"} == $node
$self->{"newest"}
$self->{"newest"} == $node
Truth table33%1/3
A and B
A= $self->{"newest"}B= $self->{"newest"} == $node
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | $self->_unlink($node); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $self->_link($node); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 3 | ▶ | sub _forget { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_forget: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 3 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 3 | $self->_unlink($node); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 3 | delete $self->{nodes}{ $node->[KEY] }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 63 | ▶ | sub _link { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_link: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 63 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 63 | $node->[OLDER] = $self->{newest}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 63 | $node->[NEWER] = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 63 | ▶ | $self->{newest}[NEWER] = $node if $self->{newest}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 63 | $self->{newest} = $node; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 63 | ▶ | $self->{oldest} = $node unless $self->{oldest}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 3 | ▶ | sub _unlink { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unlink: called CC=7 SCAR=20.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 3 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 3 | my ($older, $newer) = @{$node}[OLDER, NEWER]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 3 | ▶ | $older->[NEWER] = $newer if $older; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 3 | ▶ | $newer->[OLDER] = $older if $newer; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 3 | ▶ | $self->{newest} = $older if $self->{newest} && $self->{newest} == $node; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"newest"} and $self->{"newest"} == $node
MC/DC50%1/2
$self->{"newest"} and $self->{"newest"} == $node
$self->{"newest"}
$self->{"newest"} == $node
Truth table66%2/3
A and B
A= $self->{"newest"}B= $self->{"newest"} == $node
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 3 | ▶ | $self->{oldest} = $newer if $self->{oldest} && $self->{oldest} == $node; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"oldest"} and $self->{"oldest"} == $node
MC/DC0%0/2
$self->{"oldest"} and $self->{"oldest"} == $node
$self->{"oldest"}
$self->{"oldest"} == $node
Truth table33%1/3
A and B
A= $self->{"oldest"}B= $self->{"oldest"} == $node
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | @{$node}[OLDER, NEWER] = (undef, undef); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||