VPNDetection/Cache.pm

filter: Statementstmt 86.8% 53 / 61 Branchbran 79.1% 19 / 24 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 4 totaltotal 75.8% 88 / 116 CC 16 SCAR 14.1
CC
16
Cov
81%
_unlink
7.5
_promote
4.5
set
4.5
CRAP 4.1
« VPNDetection/Result.pm ↑ Summary VPNDetection/Bogon.pm »
1 package VPNDetection::Cache;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 our $VERSION = '3.16.0';
9  
10 # A node is [key, value, expires_at, older, newer]. The hash finds a node, the
11 # links order them by recency, so a get, a set and an eviction are each O(1). A
12 # recency ARRAY would be O(n) per touch, and once the cache is full that is every
13 # single insert.
14 7 ▶use constant { KEY => 0, VALUE => 1, EXPIRES => 2, OLDER => 3, NEWER => 4 };
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 258 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
17 258 my ($class, %args) = @_;
18 return bless {
19 max => $args{max},
20 ttl => $args{ttl},
21 258 nodes => {},
22 newest => undef,
23 oldest => undef,
24 }, $class;
25 }
26  
27 # undef is a miss. A hit is always a blessed result, so the two never collide.
28 97 ▶sub get {
Subroutine get: called CC=3 SCAR=11.0
Pod undocumented
29 97 my ($self, $key) = @_;
30 97 my $node = $self->{nodes}{$key};
31 97 ▶ return undef unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 91 6 97
32 6 ▶ if ($node->[EXPIRES] <= Time::HiRes::time()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->[2] <= &Time::HiRes::time()) 1 5 6
33 1 $self->_forget($node);
34 1 return undef;
35 }
36 5 $self->_promote($node);
37 5 return $node->[VALUE];
38 }
39  
40 45 ▶sub set {
Subroutine set: called CC=4 SCAR=15.0
Pod undocumented
41 45 my ($self, $key, $value) = @_;
42 45 ▶ return unless $self->{max} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"max"} > 0 0 45 45
43  
44 45 my $expires = Time::HiRes::time() + $self->{ttl};
45 45 ▶ if (my $node = $self->{nodes}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $node = $self->{"nodes"}{$key}) 0 45 45
46 0 @{$node}[VALUE, EXPIRES] = ($value, $expires);
47 0 $self->_promote($node);
48 0 return;
49 }
50  
51 45 my $node = [$key, $value, $expires, undef, undef];
52 45 $self->{nodes}{$key} = $node;
53 45 $self->_link($node);
54 45 ▶ $self->_forget($self->{oldest}) while keys %{$self->{nodes}} > $self->{max};
Branch: 1 branch
BranchTrueFalseTotal
keys %{$$self{"nodes"};} > $self->{"max"} and do { $self->_forget($self->{"oldest"}) } 2 45 47
55 }
56  
57 0 ▶sub size {
Subroutine size: not called CC=1 SCAR=6.9
Pod undocumented
58 0 return scalar keys %{ $_[0]{nodes} };
59 }
60  
61 5 ▶sub _promote {
Subroutine _promote: called CC=3 SCAR=15.1
62 5 my ($self, $node) = @_;
63 5 ▶ return if $self->{newest} && $self->{newest} == $node;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"newest"} and $self->{"newest"} == $node 5 0 5
Condition33%1/3
$self->{"newest"} and $self->{"newest"} == $node
!ll&&!rl&&r
005
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
ABresult
0X0
100
111
64 0 $self->_unlink($node);
65 0 $self->_link($node);
66 }
67  
68 3 ▶sub _forget {
Subroutine _forget: called CC=1 SCAR=0.0
69 3 my ($self, $node) = @_;
70 3 $self->_unlink($node);
71 3 delete $self->{nodes}{ $node->[KEY] };
72 }
73  
74 45 ▶sub _link {
Subroutine _link: called CC=3 SCAR=11.0
75 45 my ($self, $node) = @_;
76 45 $node->[OLDER] = $self->{newest};
77 45 $node->[NEWER] = undef;
78 45 ▶ $self->{newest}[NEWER] = $node if $self->{newest};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"newest"} 8 37 45
79 45 $self->{newest} = $node;
80 45 ▶ $self->{oldest} = $node unless $self->{oldest};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"oldest"} 37 8 45
81 }
82  
83 3 ▶sub _unlink {
Subroutine _unlink: called CC=7 SCAR=20.2
84 3 my ($self, $node) = @_;
85 3 my ($older, $newer) = @{$node}[OLDER, NEWER];
86 3 ▶ $older->[NEWER] = $newer if $older;
Branch: 1 branch
BranchTrueFalseTotal
if $older 0 3 3
87 3 ▶ $newer->[OLDER] = $older if $newer;
Branch: 1 branch
BranchTrueFalseTotal
if $newer 2 1 3
88 3 ▶ $self->{newest} = $older if $self->{newest} && $self->{newest} == $node;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"newest"} and $self->{"newest"} == $node 1 2 3
Condition66%2/3
$self->{"newest"} and $self->{"newest"} == $node
!ll&&!rl&&r
021
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
ABresult
0X0
100
111
89 3 ▶ $self->{oldest} = $newer if $self->{oldest} && $self->{oldest} == $node;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"oldest"} and $self->{"oldest"} == $node 3 0 3
Condition33%1/3
$self->{"oldest"} and $self->{"oldest"} == $node
!ll&&!rl&&r
003
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
ABresult
0X0
100
111
90 3 @{$node}[OLDER, NEWER] = (undef, undef);
91 }
92  
93 7 1;
94  
95 __END__
« VPNDetection/Result.pm ↑ Summary VPNDetection/Bogon.pm »