SQLEngine/Cache/BasicCache.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 145 CC 17 SCAR 28.4
CC
17
Cov
0%
get
30.0
set
20.0
expire
20.0
CRAP 17.2
1 =head1 NAME
2  
3 DBIx::SQLEngine::Cache::BasicCache - Basic Cache Object
4  
5 =head1 SYNOPSIS
6  
7 use DBIx::SQLEngine::Cache::BasicCache;
8  
9 $cache = DBIx::SQLEngine::Cache::BasicCache->new();
10  
11 $cache->set( $key, $value );
12  
13 $value = $cache->get( $key );
14  
15 $cache->clear();
16  
17 =head1 DESCRIPTION
18  
19 This package provides a fairly simple cache implementation.
20  
21 For a more full-featured cache, use one of the Cache::Cache classes.
22  
23 =cut
24  
25 package DBIx::SQLEngine::Cache::BasicCache;
26  
27 use Carp;
28  
29 ########################################################################
30  
31 =head1 CACHE INTERFACE
32  
33 =cut
34  
35 ########################################################################
36  
37 =head2 Constructor
38  
39 =over 4
40  
41 =item new()
42  
43 =back
44  
45 =cut
46  
47 # $cache = DBO::Row::Cache->new( %attrs );
48 sub new {
49 my $class = shift;
50 my $cache = bless { @_ }, $class;
51
52 $cache->{'__cache_namespace'} ||= caller();
53
54 if ( $cache->{'__cache_expiration'} ) {
55 $cache->{'__cache_last_expired'} ||= time();
56 $cache->{'__cache_expire_freq'} ||= 5;
57 }
58
59 if ( $cache->{'__cache_item_limit'} ) {
60 $cache->{'__cache_last_pruned'} ||= time();
61 $cache->{'__cache_prune_freq'} ||= 5;
62 }
63
64 return $cache;
65 }
66  
67 ########################################################################
68  
69 =head2 Accessors
70  
71 =over 4
72  
73 =item namespace()
74  
75 =back
76  
77 =cut
78  
79 use Class::MakeMethods::Standard::Hash (
80 scalar => [ qw(
81 __cache_namespace
82 __cache_expiration __cache_last_expired __cache_expire_freq
83 __cache_item_limit __cache_last_pruned __cache_prune_freq
84 ) ],
85 );
86  
87 sub get_namespace {
88 (shift)->__cache_namespace()
89 }
90  
91 ########################################################################
92  
93 =head2 Operations
94  
95 =over 4
96  
97 =item get()
98  
99 =item set()
100  
101 =item clear()
102  
103 =back
104  
105 =cut
106  
107 ########################################################################
108  
109 # $value_or_undef = $cache->get($key);
110 sub get {
111 my $cache = shift;
112 my $key = shift;
113  
114 $cache->expire() if ( $cache->{'__cache_expiration'} );
115 $cache->prune() if ( $cache->{'__cache_item_limit'} );
116  
117 unless ( exists $cache->{ $key } ) {
118 debug("RowCache","$cache->{'__cache_namespace'} - cache miss for key $key");
119 return undef;
120 }
121 my $entry = $cache->{ $key };
122 if ( $entry->{'expires'} < time() ) {
123 debug("RowCache", "$cache->{'__cache_namespace'} - cache time out for key $key");
124 $cache->clear( $key );
125 return;
126 }
127 $entry->{'last_used'} = time();
128 debug("RowCache", "$cache->{'__cache_namespace'} - cache hit for key $key");
129 return $entry->{'data'};
130 }
131  
132 # $value = $cache->set($key, $value);
133 sub set {
134 my $cache = shift;
135 my $key = shift;
136 my $data = shift;
137 return if ( not defined $key );
138 debug("RowCache", ( exists $cache->{$key} )
139 ? "$cache->{'__cache_namespace'} - overwriting key $key"
140 : "$cache->{'__cache_namespace'} - inserting key $key" );
141 $cache->{$key} = {
142 'data' => $data,
143 'last_used' => time(),
144 ( $cache->{'__cache_expiration'} ? (
145 'expires' => ( time() + $cache->{__cache_expiration} ),
146 ) : () ),
147 };
148
149 return $data;
150 }
151  
152 # $cache->clear($key);
153 sub clear {
154 my $cache = shift;
155 if ( scalar @_ ) {
156 my $key = shift;
157 delete $cache->{ $key };
158 } else {
159 debug("RowCache", "$cache->{'__cache_namespace'} - cache clear_all" );
160 %$cache = map { $_, $cache->{$_} } grep { /^__cache_/ } keys %$cache;
161 }
162 }
163  
164 ########################################################################
165  
166 =head2 Expiration and Pruning
167  
168 =over 4
169  
170 =item expire()
171  
172 =item prune()
173  
174 =back
175  
176 =cut
177  
178 # $cache->expire();
179 sub expire {
180 my $cache = shift;
181 return unless ( $cache->{'__cache_expiration'} );
182  
183 my $time = time();
184 return if (
185 $cache->{'__cache_last_expired'} + $cache->{'__cache_expire_freq'} > $time
186 );
187 $cache->{'__cache_last_expired'} = $time;
188
189 foreach my $key ( grep { $_ !~ /^__cache_/ } keys %$cache ) {
190 if ( $cache->{ $key }->{'expires'} < $time ) {
191 debug("RowCache", "$cache->{'__cache_namespace'} - expiring key $key");
192 $cache->clear( $key );
193 }
194 }
195 }
196  
197 # $cache->prune();
198 sub prune {
199 my $cache = shift;
200 return unless ( $cache->{'__cache_item_limit'} );
201  
202 my $time = time();
203 return if (
204 $cache->{'__cache_last_pruned'} + $cache->{'__cache_prune_freq'} > $time
205 );
206 $cache->{'__cache_last_pruned'} = $time;
207
208 my @keys = grep { $_ !~ /^__cache_/ } keys %$cache;
209
210 return if ( scalar @keys <= $cache->{'__cache_item_limit'} );
211
212 @keys = reverse sort {
213 $cache->{ $a }->{'last_used'} <=> $cache->{ $b }->{'last_used'}
214 } @keys;
215
216 foreach my $key ( splice @keys, $cache->{'__cache_item_limit'} ) {
217 debug("RowCache", "$cache->{'__cache_namespace'} - pruning key $key");
218 $cache->clear( $key );
219 }
220 }
221  
222 ########################################################################
223  
224 =head2 Logging
225  
226 =over 4
227  
228 =item debug()
229  
230 =back
231  
232 =cut
233  
234 sub debug {
235 warn @_
236 }
237  
238 ########################################################################
239  
240 ########################################################################
241  
242 =head1 SEE ALSO
243  
244 For a more full-featured cache, see L<Cache::Cache>.
245  
246 For more about the Cache classes, see L<DBIx::SQLEngine::Record::Trait::Cache>.
247  
248 For more about the Record classes, see L<DBIx::SQLEngine::Record::Class>.
249  
250 See L<DBIx::SQLEngine> for the overall interface and developer documentation.
251  
252 See L<DBIx::SQLEngine::Docs::ReadMe> for general information about
253 this distribution, including installation and license information.
254  
255 =cut
256  
257 ########################################################################
258  
259 1;