Articulate/Caching/Native.pm

filter: Statementstmt 72.7% 40 / 55 Branchbran 33.3% 4 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 84.6% 11 / 13 Podpod 83.3% 5 / 6 totaltotal 69.7% 60 / 86 CC 9 SCAR 14.8
CC
9
Cov
66%
get_cached
12.0
_prune
8.3
is_cached
3.1
CRAP 4.4
« Articulate/Service/Login.pm ↑ Summary Articulate/Caching.pm »
1 package Articulate::Caching::Native;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 with 'Articulate::Role::Component';
8  
9 =head1 NAME
10  
11 Articulate::Caching::Native - cache content in memory
12  
13 =head1 DESCRIPTION
14  
15 This implements caching by keeping an hash of the content you wish to
16 cache in memory.
17  
18 No attempt is made to monitor the memory size of the hash directly, but
19 a maximum number of locations under which content may be stored is set.
20 Once this maximum is reached or exceeded, a quarter of the keys are
21 removed (preserving those which have most recently been accessed).
22  
23 Consequently, it is unsuitable for cases where large documents are to
24 be stored alongside small ones, or where you have a very large number
25 of locations you want to cache.
26  
27 =cut
28  
29 0 ▶sub now { DateTime->now . '' }
Subroutine now: not called CC=1 SCAR=6.9
Pod undocumented
30  
31 =head1 ATTRIBUTES
32  
33 =cut
34  
35 =head3 cache
36  
37 This is the contents of the cache. Don't set this.
38  
39 =cut
40  
41 has cache => (
42 is => 'rw',
43 4 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
44 4 );
45  
46 =head3 max_keys
47  
48 The maximum number of keys in the hash (locations for which either meta
49 or content or both is stored).
50  
51 Be warned that each time this is exceeded, a sort is performed on the
52 values (to find the entries least recently accessed). The larger
53 max_keys is, the longer this sort will take.
54  
55 =cut
56  
57 has max_keys => (
58 is => 'rw',
59 4 ▶ default => sub { 255 },
Subroutine __ANON__: called CC=1 SCAR=0.0
60 4 );
61  
62 =head1 METHODS
63  
64 =head3 is_cached
65  
66 $caching->is_cached( $what, $location )
67  
68 =cut
69  
70 sub is_cached {
71 6 ▶ my $self = shift;
Subroutine is_cached: called CC=3 SCAR=11.2
Pod documented
72 6 my $what = shift;
73 6 my $location = shift;
74 6 ▶ return undef unless exists $self->cache->{$location};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->cache->{$location} 5 1 6
75 1 ▶ return undef unless exists $self->cache->{$location}->{$what};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->cache->{$location}{$what} 1 0 1
76 0 return 1;
77 }
78  
79 =head3 get_cached
80  
81 $caching->get_cached( $what, $location )
82  
83 =cut
84  
85 sub get_cached {
86 0 ▶ my $self = shift;
Subroutine get_cached: not called CC=3 SCAR=24.8
Pod documented
87 0 my $what = shift;
88 0 my $location = shift;
89 0 ▶ return undef unless exists $self->cache->{$location};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->cache->{$location} 0 0 0
90 0 ▶ return undef unless exists $self->cache->{$location}->{$what};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->cache->{$location}{$what} 0 0 0
91 0 $self->cache->{$location}->{last_retrieved} = now;
92 0 return $self->cache->{$location}->{$what}->{value};
93 }
94  
95 =head3 set_cache
96  
97 $caching->set_cache( $what, $location, $value )
98  
99 =cut
100  
101 sub set_cache {
102 4 ▶ my $self = shift;
Subroutine set_cache: called CC=1 SCAR=0.0
Pod documented
103 4 my $what = shift;
104 4 my $location = shift;
105 4 my $value = shift;
106 4 $self->_prune;
107 4 return $self->cache->{$location}->{$what}->{value} = $value;
108 }
109  
110 =head3 clear_cache
111  
112 $caching->clear_cache( $what, $location )
113  
114 =cut
115  
116 sub clear_cache {
117 6 ▶ my $self = shift;
Subroutine clear_cache: called CC=1 SCAR=0.0
Pod documented
118 6 my $what = shift;
119 6 my $location = shift;
120 6 return delete $self->cache->{$location}->{$what};
121 }
122  
123 =head3 empty_cache
124  
125 $caching->empty_cache( $what, $location )
126  
127 =cut
128  
129 sub empty_cache {
130 6 ▶ my $self = shift;
Subroutine empty_cache: called CC=2 SCAR=6.9
Pod documented
131 6 delete $self->cache->{$_} for keys %{ $self->cache };
132 }
133  
134 sub _prune {
135 4 ▶ my $self = shift;
Subroutine _prune: called CC=4 SCAR=21.1
136 4 my $to_remove = ( keys %{ $self->cache } ) - $self->max_keys;
137 4 ▶ if ( $to_remove > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_remove > 1) 0 4 4
138 0 $to_remove = $to_remove +
139 int( $self->max_keys / 4 ); # so we don't have to do this too often
140 0 foreach my $location (
141 sort {
142 $self->cached->{$a}->{last_retrieved}
143 cmp $self->cached->{$b}->{last_retrieved}
144 0 } keys %{ $self->cached }
145 )
146 {
147 0 delete $self->cached->{$location};
148 0 ▶ last unless --$to_remove;
Branch: 1 branch
BranchTrueFalseTotal
unless --$to_remove 0 0 0
149 }
150 }
151 }
152  
153 4 1;
« Articulate/Service/Login.pm ↑ Summary Articulate/Caching.pm »