Ref/LRU.pm

filter: Statementstmt 100% 51 / 51 Branchbran 100% 6 / 6 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 12 / 12 Podpod 100% 6 / 6 totaltotal 95.1% 79 / 83 CC 7 SCAR 7.0
CC
7
Cov
97%
expire
3.0
get
3.0
set
3.0
CRAP 2.0
« Ref/Role/API.pm ↑ Summary Ref/Util/LRU/Array.pm »
1 package Cache::Ref::LRU;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $Cache::Ref::LRU::AUTHORITY = 'cpan:NUFFIN';
4 }
5 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 $Cache::Ref::LRU::VERSION = '0.05'; # TRIAL
7 }
8 # ABSTRACT: Least recently used expiry policy
9  
10 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Cache::Ref::Util::LRU::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 extends qw(Cache::Ref);
17  
18 1 with qw(
19 Cache::Ref::Role::API
20 Cache::Ref::Role::Index
21 );
22  
23 1 has size => (
24 isa => "Int",
25 is => "ro",
26 required => 1,
27 );
28  
29 1 has lru_class => (
30 isa => "ClassName",
31 is => "ro",
32 default => "Cache::Ref::Util::LRU::List",
33 );
34  
35 1 has _lru => (
36 does => "Cache::Ref::Util::LRU::API",
37 is => "ro",
38 lazy_build => 1,
39 );
40  
41 4 ▶sub _build__lru { shift->lru_class->new }
Subroutine _build__lru: called CC=1 SCAR=0.0
42  
43 6048 ▶sub get {
Subroutine get: called CC=3 SCAR=11.0
Pod documented
44 6048 my ( $self, @keys ) = @_;
45  
46 6048 my @e = $self->_index_get(@keys);
47  
48 6048 $self->_lru->hit(map { $_->[1] } grep { defined } @e);
49  
50 6048 ▶ return ( @keys == 1 ? $e[0][0] : map { $_ && $_->[0] } @e );
Branch: 1 branch
BranchTrueFalseTotal
@keys == 1 ? : 6046 2 6048
Condition66%2/3
$_ && $_->[0]
!ll&&!rl&&r
204
MC/DC50%1/2
$_ && $_->[0]
$_ $_->[0]
Truth table66%2/3
A && B
A= $_B= $_->[0]
ABresult
0X0
100
111
51 }
52  
53 2 ▶sub hit {
Subroutine hit: called CC=1 SCAR=0.0
Pod documented
54 2 my ( $self, @keys ) = @_;
55  
56 2 $self->_lru->hit( map { $_->[1] } $self->_index_get(@keys) );
57  
58 2 return;
59 }
60  
61 3534 ▶sub expire {
Subroutine expire: called CC=3 SCAR=11.1
Pod documented
62 3534 my ( $self, $how_many ) = @_;
63  
64 3534 my $l = $self->_lru;
65 3534 ▶ $self->_index_delete( $l->remove_lru ) for 1 .. ($how_many || 1);
Condition50%1/2
$how_many || 1
l!l
35340
MC/DC0%0/1
$how_many || 1
$how_many
Truth table50%1/2
A
A= $how_many
Aresult
11
00
66  
67 3534 return;
68 }
69  
70 3550 ▶sub set {
Subroutine set: called CC=3 SCAR=11.0
Pod documented
71 3550 my ( $self, $key, $value ) = @_;
72  
73 3550 my $l = $self->_lru;
74  
75 3550 ▶ if ( my $e = $self->_index_get($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $self->_index_get($key)) { } 2 3548 3550
76 2 $l->hit($e->[1]);
77 2 $e->[0] = $value;
78 } else {
79 3548 ▶ if ( $self->_index_size == $self->size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_index_size == $self->size) 3532 16 3548
80 3532 $self->expire(1);
81 }
82  
83 3548 $self->_index_set( $key => [ $value, $l->insert($key) ] );
84 }
85  
86 3550 return $value;
87 }
88  
89 2 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
90 2 my $self = shift;
91  
92 2 $self->_lru->clear;
93 2 $self->_index_clear;
94  
95 2 return;
96 }
97  
98 2 ▶sub remove {
Subroutine remove: called CC=1 SCAR=0.0
Pod documented
99 2 my ( $self, @keys ) = @_;
100  
101 2 $self->_lru->remove(map { $_->[1] } $self->_index_delete(@keys));
102  
103 2 return;
104 }
105  
106 1 __PACKAGE__->meta->make_immutable;
107  
108 1 __PACKAGE__;
109  
110  
111 # ex: set sw=4 et:
112  
113 __END__
« Ref/Role/API.pm ↑ Summary Ref/Util/LRU/Array.pm »