DBI/Record/Cachable.pm

filter: Statementstmt 79.6% 86 / 108 Branchbran 73.8% 31 / 42 Conditioncond 24.0% 6 / 25 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 78.2% 18 / 23 Podpod 100% 3 / 3 totaltotal 66.3% 144 / 217 CC 31 SCAR 17.6
CC
31
Cov
70%
load_from_hash
11.8
load_by_cols
10.4
_gen_record_cache_key
9.0
CRAP 5.8
« DBI/Handle/Informix.pm ↑ Summary DBI/Filter/Truncate.pm »
1 package Jifty::DBI::Record::Cachable;
2  
3 2 ▶use base qw(Jifty::DBI::Record);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Jifty::DBI::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Cache::Simple::TimedExpiry;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Scalar::Util qw/ blessed /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Jifty::DBI::Record::Cachable - records with caching behavior
16  
17 =head1 SYNOPSIS
18  
19 package Myrecord;
20 use base qw/Jifty::DBI::Record::Cachable/;
21  
22 =head1 DESCRIPTION
23  
24 This module subclasses the main L<Jifty::DBI::Record> package to add a
25 caching layer.
26  
27 The public interface remains the same, except that records which have
28 been loaded in the last few seconds may be reused by subsequent fetch
29 or load methods without retrieving them from the database.
30  
31 =head1 METHODS
32  
33 =cut
34  
35 2 my %_CACHES = ();
36  
37 sub _setup_cache {
38 9 ▶ my $self = shift;
Subroutine _setup_cache: called CC=1 SCAR=0.0
39 9 my $cache = shift;
40 9 $_CACHES{$cache} = Cache::Simple::TimedExpiry->new();
41 9 $_CACHES{$cache}->expire_after( $self->_cache_config->{'cache_for_sec'} );
42 }
43  
44 =head2 flush_cache
45  
46 This class method flushes the _global_ Jifty::DBI::Record::Cachable
47 cache. All caches are immediately expired.
48  
49 =cut
50  
51 sub flush_cache {
52 6 ▶ %_CACHES = ();
Subroutine flush_cache: called CC=1 SCAR=0.0
Pod documented
53 }
54  
55 sub _key_cache {
56 15 ▶ my $self = shift;
Subroutine _key_cache: called CC=3 SCAR=11.3
57 my $cache
58 = $self->_handle->dsn
59 . "-KEYS--"
60 15 ▶ . ( $self->{'_class'} || $self->table );
Condition33%1/3
$self->{'_class'} || $self->table
l!l&&r!l&&!r
0150
MC/DC0%0/2
$self->{'_class'} || $self->table
$self->{'_class'} $self->table
Truth table33%1/3
A || B
A= $self->{'_class'}B= $self->table
ABresult
1X1
011
000
61 15 ▶ $self->_setup_cache($cache) unless exists( $_CACHES{$cache} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_CACHES{$cache} 4 11 15
62 15 return ( $_CACHES{$cache} );
63  
64 }
65  
66 =head2 _flush_key_cache
67  
68 Blow away this record type's key cache
69  
70 =cut
71  
72 sub _flush_key_cache {
73 0 ▶ my $self = shift;
Subroutine _flush_key_cache: not called CC=2 SCAR=17.9
74 my $cache
75 = $self->_handle->dsn
76 . "-KEYS--"
77 0 ▶ . ( $self->{'_class'} || $self->table );
Condition0%0/3
$self->{'_class'} || $self->table
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'_class'} || $self->table
$self->{'_class'} $self->table
Truth table0%0/3
A || B
A= $self->{'_class'}B= $self->table
ABresult
1X1
011
000
78 0 $self->_setup_cache($cache);
79 }
80  
81 sub _record_cache {
82 20 ▶ my $self = shift;
Subroutine _record_cache: called CC=3 SCAR=11.3
83 my $cache
84 20 ▶ = $self->_handle->dsn . "--" . ( $self->{'_class'} || $self->table );
Condition33%1/3
$self->{'_class'} || $self->table
l!l&&r!l&&!r
0200
MC/DC0%0/2
$self->{'_class'} || $self->table
$self->{'_class'} $self->table
Truth table33%1/3
A || B
A= $self->{'_class'}B= $self->table
ABresult
1X1
011
000
85 20 ▶ $self->_setup_cache($cache) unless exists( $_CACHES{$cache} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_CACHES{$cache} 5 15 20
86 20 return ( $_CACHES{$cache} );
87  
88 }
89  
90 sub _is_in_transaction {
91 8 ▶ my $self = shift;
Subroutine _is_in_transaction: called CC=1 SCAR=0.0
92 8 $Jifty::DBI::Handle::TRANSDEPTH > 0;
93 }
94  
95 =head2 load_from_hash
96  
97 Overrides the implementation from L<Jifty::DBI::Record> to add caching.
98  
99 =cut
100  
101 sub load_from_hash {
102 1 ▶ my $self = shift;
Subroutine load_from_hash: called CC=6 SCAR=24.7
Pod documented
103  
104 1 my ( $rvalue, $msg );
105 1 ▶ if ( ref($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 1 0 1
106  
107 # Blow away the primary cache key since we're loading.
108 1 $self->{'_jifty_cache_pkey'} = undef;
109 1 ( $rvalue, $msg ) = $self->SUPER::load_from_hash(@_);
110  
111 ## Check the return value, if its good, cache it!
112 1 ▶ $self->_store() if ($rvalue && !$self->_is_in_transaction);
Branch: 1 branch
BranchTrueFalseTotal
if $rvalue and not $self->_is_in_transaction 1 0 1
Condition33%1/3
$rvalue and not $self->_is_in_transaction
!ll&&!rl&&r
001
MC/DC0%0/2
$rvalue and not $self->_is_in_transaction
$rvalue not $self->_is_in_transaction
Truth table33%1/3
A and not(B)
A= $rvalueB= not $self->_is_in_transaction
ABresult
0X0
100
111
113 1 return ( $rvalue, $msg );
114 } else { # Called as a class method;
115 0 $self = $self->SUPER::load_from_hash(@_);
116 ## Check the return value, if its good, cache it!
117 0 ▶ $self->_store() if ( $self->id && !$self->_is_in_transaction );
Branch: 1 branch
BranchTrueFalseTotal
if $self->id and not $self->_is_in_transaction 0 0 0
Condition0%0/3
$self->id and not $self->_is_in_transaction
!ll&&!rl&&r
000
MC/DC0%0/2
$self->id and not $self->_is_in_transaction
$self->id not $self->_is_in_transaction
Truth table0%0/3
A and not(B)
A= $self->idB= not $self->_is_in_transaction
ABresult
0X0
100
111
118 0 return ($self);
119 }
120  
121 }
122  
123 =head2 load_by_cols
124  
125 Overrides the implementation from L<Jifty::DBI::Record> to add caching.
126  
127 =cut
128  
129 sub load_by_cols {
130 11 ▶ my ( $class, %attr ) = @_;
Subroutine load_by_cols: called CC=8 SCAR=23.4
Pod documented
131  
132 11 my ($self);
133 11 ▶ if ( ref($class) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) { } 11 0 11
134 11 ( $self, $class ) = ( $class, undef );
135 } else {
136 0 ▶ $self = $class->new( handle => ( delete $attr{'_handle'} || undef ) );
Condition0%0/2
delete $attr{'_handle'} || undef
l!l
00
MC/DC0%0/1
delete $attr{'_handle'} || undef
delete $attr{'_handle'}
Truth table0%0/2
A
A= delete $attr{'_handle'}
Aresult
11
00
137 }
138  
139 ## Generate the cache key
140 11 my $alt_key = $self->_gen_record_cache_key(%attr);
141 11 ▶ if ( $self->_fetch($alt_key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_fetch($alt_key)) 4 7 11
142 4 ▶ if ($class) { return $self }
Branch: 1 branch
BranchTrueFalseTotal
if ($class) { } 0 4 4
143 4 else { return ( 1, "Fetched from cache" ) }
144 }
145  
146 # Blow away the primary cache key since we're loading.
147 7 $self->{'_jifty_cache_pkey'} = undef;
148  
149 ## Fetch from the DB!
150 7 my ( $rvalue, $msg ) = $self->SUPER::load_by_cols(%attr);
151 ## Check the return value, if its good, cache it!
152 7 ▶ if ($rvalue && !$self->_is_in_transaction) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rvalue and not $self->_is_in_transaction) 7 0 7
Condition33%1/3
$rvalue and not $self->_is_in_transaction
!ll&&!rl&&r
007
MC/DC0%0/2
$rvalue and not $self->_is_in_transaction
$rvalue not $self->_is_in_transaction
Truth table33%1/3
A and not(B)
A= $rvalueB= not $self->_is_in_transaction
ABresult
0X0
100
111
153 ## Only cache the object if its okay to do so.
154 7 $self->_store();
155 7 $self->_key_cache->set(
156 $alt_key => $self->_primary_record_cache_key );
157  
158 }
159 7 ▶ if ($class) { return $self }
Branch: 1 branch
BranchTrueFalseTotal
if ($class) { } 0 7 7
160 else {
161 7 return ( $rvalue, $msg );
162 }
163 }
164  
165 # Function: __set
166 # Type : (overloaded) public instance
167 # Args : see Jifty::DBI::Record::_Set
168 # Lvalue : ?
169  
170 sub __set () {
171 0 ▶ my $self = shift;
Subroutine __set: not called CC=1 SCAR=6.9
172  
173 0 $self->_expire();
174 0 return $self->SUPER::__set(@_);
175  
176 }
177  
178 # Function: delete
179 # Type : (overloaded) public instance
180 # Args : nil
181 # Lvalue : ?
182  
183 sub __delete () {
184 0 ▶ my $self = shift;
Subroutine __delete: not called CC=1 SCAR=6.9
185  
186 0 $self->_expire();
187 0 return $self->SUPER::__delete(@_);
188  
189 }
190  
191 # Function: _expire
192 # Type : private instance
193 # Args : string(cache_key)
194 # Lvalue : 1
195 # Desc : Removes this object from the cache.
196  
197 sub _expire (\$) {
198 0 ▶ my $self = shift;
Subroutine _expire: not called CC=1 SCAR=6.9
199 0 $self->_record_cache->set( $self->_primary_record_cache_key,
200 undef, time - 1 );
201  
202 # We should be doing something more surgical to clean out the key cache. but we do need to expire it
203 0 $self->_flush_key_cache;
204  
205 }
206  
207 # Function: _fetch
208 # Type : private instance
209 # Args : string(cache_key)
210 # Lvalue : 1
211 # Desc : Get an object from the cache, and make this object that.
212  
213 sub _fetch () {
214 11 ▶ my ( $self, $cache_key ) = @_;
Subroutine _fetch: called CC=4 SCAR=13.9
215  
216 # If the alternate key is really the primary one
217  
218 11 my $data = $self->_record_cache->fetch($cache_key);
219  
220 11 ▶ unless ($data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 8 3 11
221 8 $cache_key = $self->_key_cache->fetch($cache_key);
222 8 ▶ $data = $self->_record_cache->fetch($cache_key) if $cache_key;
Branch: 1 branch
BranchTrueFalseTotal
if $cache_key 1 7 8
223 }
224  
225 11 ▶ return undef unless ($data);
Branch: 1 branch
BranchTrueFalseTotal
unless $data 7 4 11
226  
227 4 @{$self}{ keys %$data } = values %$data; # deserialize
228 4 return 1;
229 }
230  
231 #sub __value {
232 # my $self = shift;
233 # my $column = shift;
234 #
235 # # XXX TODO, should we be fetching directly from the cache?
236 # return ( $self->SUPER::__value($column) );
237 #}
238  
239 # Function: _store
240 # Type : private instance
241 # Args : string(cache_key)
242 # Lvalue : 1
243 # Desc : Stores this object in the cache.
244  
245 sub _store (\$) {
246 8 ▶ my $self = shift;
Subroutine _store: called CC=1 SCAR=0.0
247 $self->_record_cache->set(
248 $self->_primary_record_cache_key,
249 { values => $self->{'values'},
250 table => $self->table,
251 fetched => $self->{'fetched'},
252 decoded => $self->{'decoded'},
253 8 raw_values => $self->{'raw_values'},
254 }
255 );
256 }
257  
258 # Function: _gen_record_cache_key
259 # Type : private instance
260 # Args : hash (attr)
261 # Lvalue : 1
262 # Desc : Takes a perl hash and generates a key from it.
263  
264 sub _gen_record_cache_key {
265 11 ▶ my ( $self, %attr ) = @_;
Subroutine _gen_record_cache_key: called CC=8 SCAR=22.0
266  
267 11 my @cols;
268  
269 11 ▶ while ( my ( $key, $value ) = each %attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %attr) 11 11 22
270 11 ▶ unless ( defined $value ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $value) { } 10 1 11
if (do { ref $value eq 'HASH' }) { } 1 9 10
elsif (builtin::blessed($value) and $value->isa('Jifty::DBI::Record')) { } 0 9 9
271 1 push @cols, lc($key) . '=__undef';
272 } elsif ( ref($value) eq "HASH" ) {
273 push @cols,
274 lc($key)
275 . ( $value->{operator} || '=' )
276 1 ▶ . defined $value->{value} ? $value->{value} : '__undef';
Branch: 1 branch
BranchTrueFalseTotal
lc($key) . ($value->{'operator'} || '=') . defined($value->{'value'}) ? : 1 0 1
Condition50%1/2
$value->{'operator'} || '='
l!l
10
MC/DC0%0/1
$value->{'operator'} || '='
$value->{'operator'}
Truth table50%1/2
A
A= $value->{'operator'}
Aresult
11
00
277 ▶ } elsif ( blessed $value and $value->isa('Jifty::DBI::Record') ) {
Condition33%1/3
builtin::blessed($value) and $value->isa("Jifty::DBI::Record")
!ll&&!rl&&r
900
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Jifty::DBI::Record")
builtin::blessed($value) $value->isa("Jifty::DBI::Record")
Truth table33%1/3
A and B
A= builtin::blessed($value)B= $value->isa("Jifty::DBI::Record")
ABresult
0X0
100
111
278 0 push @cols, lc($key) . '=' . ( $value->id );
279 } else {
280 9 push @cols, lc($key) . "=" . $value;
281 }
282 }
283 11 return ( $self->table() . ':' . join( ',', @cols ) );
284 }
285  
286 # Function: _fetch_record_cache_key
287 # Type : private instance
288 # Args : nil
289 # Lvalue : 1
290  
291 sub _fetch_record_cache_key {
292 0 ▶ my ($self) = @_;
Subroutine _fetch_record_cache_key: not called CC=1 SCAR=6.9
293 0 my $cache_key = $self->_cache_config->{'cache_key'};
294 0 return ($cache_key);
295 }
296  
297 # Function: _primary_record_cache_key
298 # Type : private instance
299 # Args : none
300 # Lvalue: : 1
301 # Desc : generate a primary-key based variant of this object's cache key
302 # primary keys is in the cache
303  
304 sub _primary_record_cache_key {
305 15 ▶ my ($self) = @_;
Subroutine _primary_record_cache_key: called CC=4 SCAR=13.9
306  
307 15 ▶ unless ( $self->{'_jifty_cache_pkey'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_jifty_cache_pkey"}) 8 7 15
308  
309 8 my @attributes;
310 8 my %pk = $self->primary_keys;
311 8 ▶ while ( my ( $key, $value ) = each %pk ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %pk) 8 8 16
312 8 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 8 8
313 8 push @attributes, lc($key) . '=' . $value;
314 }
315  
316 8 $self->{'_jifty_cache_pkey'} = $self->table . ':' . join ',',
317 @attributes;
318 }
319 15 return ( $self->{'_jifty_cache_pkey'} );
320  
321 }
322  
323 =head2 _cache_config
324  
325 You can override this method to change the duration of the caching
326 from the default of 5 seconds.
327  
328 For example, to cache records for up to 30 seconds, add the following
329 method to your class:
330  
331 sub _cache_config {
332 { 'cache_for_sec' => 30 }
333 }
334  
335 =cut
336  
337 sub _cache_config {
338 9 ▶ { 'cache_p' => 1,
Subroutine _cache_config: called CC=1 SCAR=0.0
339 'cache_for_sec' => 5,
340 };
341 }
342  
343 2 1;
344  
345 __END__
346  
347  
348 =head1 AUTHOR
349  
350 Matt Knopp <mhat@netlag.com>
351  
352 =head1 SEE ALSO
353  
354 L<Jifty::DBI>, L<Jifty::DBI::Record>
355  
356 =cut
357  
358  
« DBI/Handle/Informix.pm ↑ Summary DBI/Filter/Truncate.pm »