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.14.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 251 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
17 251 my ($class, %args) = @_;
18 return bless {
19 max => $args{max},
20 ttl => $args{ttl},
21 251 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 96 ▶sub get {
Subroutine get: called CC=3 SCAR=11.0
Pod undocumented
29 96 my ($self, $key) = @_;
30 96 my $node = $self->{nodes}{$key};
31 96 ▶ return undef unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 90 6 96
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 44 ▶sub set {
Subroutine set: called CC=4 SCAR=15.0
Pod undocumented
41 44 my ($self, $key, $value) = @_;
42 44 ▶ return unless $self->{max} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"max"} > 0 0 44 44
43  
44 44 my $expires = Time::HiRes::time() + $self->{ttl};
45 44 ▶ if (my $node = $self->{nodes}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $node = $self->{"nodes"}{$key}) 0 44 44
46 0 @{$node}[VALUE, EXPIRES] = ($value, $expires);
47 0 $self->_promote($node);
48 0 return;
49 }
50  
51 44 my $node = [$key, $value, $expires, undef, undef];
52 44 $self->{nodes}{$key} = $node;
53 44 $self->_link($node);
54 44 ▶ $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 44 46
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 44 ▶sub _link {
Subroutine _link: called CC=3 SCAR=11.0
75 44 my ($self, $node) = @_;
76 44 $node->[OLDER] = $self->{newest};
77 44 $node->[NEWER] = undef;
78 44 ▶ $self->{newest}[NEWER] = $node if $self->{newest};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"newest"} 8 36 44
79 44 $self->{newest} = $node;
80 44 ▶ $self->{oldest} = $node unless $self->{oldest};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"oldest"} 36 8 44
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 »