| 1 | package VPNDetection::Cache; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | 15 | our $VERSION = '4.0.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 | 15 | ▶ | 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 capped answer, the basics with cap_reached true, is held, in seconds. | ||||||||||||||||||||||||||||||||||
| 18 | 15 | ▶ | use constant REDUCED_TTL => 60; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | 320 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 21 | 320 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||
| 22 | return bless { | ||||||||||||||||||||||||||||||||||
| 23 | max => $args{max}, | ||||||||||||||||||||||||||||||||||
| 24 | ttl => $args{ttl}, | ||||||||||||||||||||||||||||||||||
| 25 | 320 | 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 | 1218 | ▶ | sub get { | ||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 33 | 1218 | my ($self, $key) = @_; | |||||||||||||||||||||||||||||||||
| 34 | 1218 | my $node = $self->{nodes}{$key}; | |||||||||||||||||||||||||||||||||
| 35 | 1218 | ▶ | return undef unless $node; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 36 | 34 | ▶ | if ($node->[EXPIRES] <= Time::HiRes::time()) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 37 | 5 | $self->_forget($node); | |||||||||||||||||||||||||||||||||
| 38 | 5 | return undef; | |||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||
| 40 | 29 | $self->_promote($node); | |||||||||||||||||||||||||||||||||
| 41 | 29 | return $node->[VALUE]; | |||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | 1125 | ▶ | sub set { | ||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=4 SCAR=15.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 45 | 1125 | my ($self, $key, $value) = @_; | |||||||||||||||||||||||||||||||||
| 46 | 1125 | ▶ | return unless $self->{max} > 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||
| 48 | 1125 | my $expires = Time::HiRes::time() + $self->_life_of($value); | |||||||||||||||||||||||||||||||||
| 49 | 1125 | ▶ | 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 | 1125 | my $node = [$key, $value, $expires, undef, undef]; | |||||||||||||||||||||||||||||||||
| 56 | 1125 | $self->{nodes}{$key} = $node; | |||||||||||||||||||||||||||||||||
| 57 | 1125 | $self->_link($node); | |||||||||||||||||||||||||||||||||
| 58 | 1125 | ▶ | $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 capped answer can change as soon as the organization's plan or cap does, or the | ||||||||||||||||||||||||||||||||||
| 73 | # UTC day turns, 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, and | ||||||||||||||||||||||||||||||||||
| 75 | # every hit raises again. Only cap_reached shortens it. | ||||||||||||||||||||||||||||||||||
| 76 | 1125 | ▶ | sub _life_of { | ||||||||||||||||||||||||||||||||
|
Subroutine
_life_of: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||||||||
| 77 | 1125 | my ($self, $value) = @_; | |||||||||||||||||||||||||||||||||
| 78 | 1125 | ▶ | my $capped = Scalar::Util::blessed($value) && $value->can('cap_reached') && $value->cap_reached; | ||||||||||||||||||||||||||||||||
|
Condition50%3/6
builtin::blessed($value) && $value->can('cap_reached') && $value->cap_reached
builtin::blessed($value) and $value->can("cap_reached")
MC/DC33%1/3
builtin::blessed($value) && $value->can('cap_reached') && $value->cap_reached
builtin::blessed($value)
$value->can("cap_reached")
$value->cap_reached
Truth table50%2/4
A and B && C
A= builtin::blessed($value)B= $value->can("cap_reached")C= $value->cap_reached
| |||||||||||||||||||||||||||||||||||
| 79 | 1125 | ▶ | return $self->{ttl} unless $capped; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 80 | 20 | ▶ | return $self->{ttl} < REDUCED_TTL ? $self->{ttl} : REDUCED_TTL; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||
| 83 | 29 | ▶ | sub _promote { | ||||||||||||||||||||||||||||||||
|
Subroutine
_promote: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 84 | 29 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||
| 85 | 29 | ▶ | return 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
| |||||||||||||||||||||||||||||||||||
| 86 | 8 | $self->_unlink($node); | |||||||||||||||||||||||||||||||||
| 87 | 8 | $self->_link($node); | |||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||
| 90 | 10 | ▶ | sub _forget { | ||||||||||||||||||||||||||||||||
|
Subroutine
_forget: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 91 | 10 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||
| 92 | 10 | $self->_unlink($node); | |||||||||||||||||||||||||||||||||
| 93 | 10 | delete $self->{nodes}{ $node->[KEY] }; | |||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||
| 96 | 1133 | ▶ | sub _link { | ||||||||||||||||||||||||||||||||
|
Subroutine
_link: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 97 | 1133 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||
| 98 | 1133 | $node->[OLDER] = $self->{newest}; | |||||||||||||||||||||||||||||||||
| 99 | 1133 | $node->[NEWER] = undef; | |||||||||||||||||||||||||||||||||
| 100 | 1133 | ▶ | $self->{newest}[NEWER] = $node if $self->{newest}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 101 | 1133 | $self->{newest} = $node; | |||||||||||||||||||||||||||||||||
| 102 | 1133 | ▶ | $self->{oldest} = $node unless $self->{oldest}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||
| 105 | 18 | ▶ | sub _unlink { | ||||||||||||||||||||||||||||||||
|
Subroutine
_unlink: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||
| 106 | 18 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||
| 107 | 18 | my ($older, $newer) = @{$node}[OLDER, NEWER]; | |||||||||||||||||||||||||||||||||
| 108 | 18 | ▶ | $older->[NEWER] = $newer if $older; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 109 | 18 | ▶ | $newer->[OLDER] = $older if $newer; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 110 | 18 | ▶ | $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 | 18 | ▶ | $self->{oldest} = $newer if $self->{oldest} && $self->{oldest} == $node; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"oldest"} and $self->{"oldest"} == $node
MC/DC50%1/2
$self->{"oldest"} and $self->{"oldest"} == $node
$self->{"oldest"}
$self->{"oldest"} == $node
Truth table66%2/3
A and B
A= $self->{"oldest"}B= $self->{"oldest"} == $node
| |||||||||||||||||||||||||||||||||||
| 112 | 18 | @{$node}[OLDER, NEWER] = (undef, undef); | |||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||
| 115 | 15 | 1; | |||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||
| 117 | __END__ | ||||||||||||||||||||||||||||||||||