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.61';
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 481 ▶sub BUILD {
Subroutine BUILD: called CC=5 SCAR=16.1
Pod undocumented
26 481 my ( $self, $params ) = @_;
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 9112 ▶sub fetch {
Subroutine fetch: called CC=2 SCAR=6.9
Pod undocumented
40 9112 my ( $self, $key ) = @_;
41  
42 9112 ▶ if ( $self->{is_size_aware} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"is_size_aware"}) 2928 6184 9112
43 2928 $self->{datastore}->{ CHI_Meta_Namespace() }->{last_used_time}->{$key}
44 = time;
45 }
46 9112 return $self->{datastore}->{ $self->{namespace} }->{$key};
47 }
48  
49 3523 ▶sub store {
Subroutine store: called CC=1 SCAR=0.0
Pod undocumented
50 3523 my ( $self, $key, $data ) = @_;
51  
52 3523 $self->{datastore}->{ $self->{namespace} }->{$key} = $data;
53 }
54  
55 990 ▶sub remove {
Subroutine remove: called CC=1 SCAR=0.0
Pod undocumented
56 990 my ( $self, $key ) = @_;
57  
58 990 delete $self->{datastore}->{ $self->{namespace} }->{$key};
59 delete $self->{datastore}->{ CHI_Meta_Namespace() }->{last_used_time}
60 990 ->{$key};
61 }
62  
63 531 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
64 531 my ($self) = @_;
65  
66 531 $self->{datastore}->{ $self->{namespace} } = {};
67 }
68  
69 885 ▶sub get_keys {
Subroutine get_keys: called CC=1 SCAR=0.0
Pod undocumented
70 885 my ($self) = @_;
71  
72 885 return keys( %{ $self->{datastore}->{ $self->{namespace} } } );
73 }
74  
75 33 ▶sub get_namespaces {
Subroutine get_namespaces: called CC=1 SCAR=0.0
Pod undocumented
76 33 my ($self) = @_;
77  
78 33 return keys( %{ $self->{datastore} } );
79 }
80  
81 204 ▶sub discard_policy_lru {
Subroutine discard_policy_lru: called CC=1 SCAR=0.0
Pod undocumented
82 204 my ($self) = @_;
83  
84 my $last_used_time =
85 204 $self->{datastore}->{ CHI_Meta_Namespace() }->{last_used_time};
86 my @keys_in_lru_order =
87 204 sort { $last_used_time->{$a} <=> $last_used_time->{$b} } $self->get_keys;
88 424 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
89 424 shift(@keys_in_lru_order);
90 204 };
91 }
92  
93 19 1;
94  
95 __END__
« CHI/Test/Class.pm ↑ Summary CHI/t/Config.pm »