CHI/Driver/Memory.pm

filter: Statementstmt 100% 55 / 55 Branchbran 90.0% 9 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 9 totaltotal 88.8% 80 / 90 CC 6 SCAR 7.2
CC
6
Cov
98%
BUILD
5.0
fetch
2.0
BEGIN
1.0
CRAP 2.0
« CHI/Test/Class.pm ↑ Summary CHI/t/Config.pm »
1 package CHI::Driver::Memory;
2 19 $CHI::Driver::Memory::VERSION = '0.60';
3 19 ▶use Carp qw(cluck croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use CHI::Constants qw(CHI_Meta_Namespace);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 19 extends 'CHI::Driver';
11  
12 19 our %Global_Datastore = (); ## no critic (ProhibitPackageVars)
13  
14 19 has 'datastore' => ( is => 'ro', isa => HashRef );
15 19 has 'global' => ( is => 'ro', isa => Bool );
16  
17 29 ▶sub default_discard_policy { 'lru' }
Subroutine default_discard_policy: called CC=1 SCAR=0.0
Pod undocumented
18  
19 # We see a lot of repeated '$self->{datastore}->{$self->{namespace}}'
20 # expressions below. The reason this cannot be easily memoized in the object
21 # is that we want the cache to be cleared across multiple existing CHI
22 # objects when the datastore itself is emptied - e.g. %datastore = ()
23 #
24  
25 sub BUILD {
26 481 ▶ my ( $self, $params ) = @_;
Subroutine BUILD: called CC=5 SCAR=16.1
Pod undocumented
27  
28 481 ▶ if ( defined $self->{global} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"global"}) 376 105 481
29 croak "cannot specify both 'datastore' and 'global'"
30 376 ▶ if ( defined( $self->{datastore} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"datastore"} 0 376 376
31 376 ▶ $self->{datastore} = $self->{global} ? \%Global_Datastore : {};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'global'} ? : 370 6 376
32 }
33 481 ▶ if ( !defined( $self->{datastore} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"datastore"}) 6 475 481
34 6 cluck "must specify either 'datastore' hashref or 'global' flag";
35 6 $self->{datastore} = \%Global_Datastore;
36 }
37 }
38  
39 sub fetch {
40 9121 ▶ my ( $self, $key ) = @_;
Subroutine fetch: called CC=2 SCAR=6.9
Pod undocumented
41  
42 9121 ▶ if ( $self->{is_size_aware} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"is_size_aware"}) 2937 6184 9121
43 2937 $self->{datastore}->{ CHI_Meta_Namespace() }->{last_used_time}->{$key}
44 = time;
45 }
46 9121 return $self->{datastore}->{ $self->{namespace} }->{$key};
47 }
48  
49 sub store {
50 3516 ▶ my ( $self, $key, $data ) = @_;
Subroutine store: called CC=1 SCAR=0.0
Pod undocumented
51  
52 3516 $self->{datastore}->{ $self->{namespace} }->{$key} = $data;
53 }
54  
55 sub remove {
56 1000 ▶ my ( $self, $key ) = @_;
Subroutine remove: called CC=1 SCAR=0.0
Pod undocumented
57  
58 1000 delete $self->{datastore}->{ $self->{namespace} }->{$key};
59 delete $self->{datastore}->{ CHI_Meta_Namespace() }->{last_used_time}
60 1000 ->{$key};
61 }
62  
63 sub clear {
64 531 ▶ my ($self) = @_;
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
65  
66 531 $self->{datastore}->{ $self->{namespace} } = {};
67 }
68  
69 sub get_keys {
70 889 ▶ my ($self) = @_;
Subroutine get_keys: called CC=1 SCAR=0.0
Pod undocumented
71  
72 889 return keys( %{ $self->{datastore}->{ $self->{namespace} } } );
73 }
74  
75 sub get_namespaces {
76 33 ▶ my ($self) = @_;
Subroutine get_namespaces: called CC=1 SCAR=0.0
Pod undocumented
77  
78 33 return keys( %{ $self->{datastore} } );
79 }
80  
81 sub discard_policy_lru {
82 208 ▶ my ($self) = @_;
Subroutine discard_policy_lru: called CC=1 SCAR=0.0
Pod undocumented
83  
84 my $last_used_time =
85 208 $self->{datastore}->{ CHI_Meta_Namespace() }->{last_used_time};
86 my @keys_in_lru_order =
87 208 sort { $last_used_time->{$a} <=> $last_used_time->{$b} } $self->get_keys;
88 return sub {
89 432 ▶ shift(@keys_in_lru_order);
Subroutine __ANON__: called CC=1 SCAR=0.0
90 208 };
91 }
92  
93 19 1;
94  
95 __END__
« CHI/Test/Class.pm ↑ Summary CHI/t/Config.pm »