SearchBuilder/Record/Cachable.pm

filter: Statementstmt 72.7% 72 / 99 Branchbran 43.3% 13 / 30 Conditioncond 41.3% 12 / 29 MC/DCmcdc 5.2% 1 / 19 Subroutinesub 69.5% 16 / 23 Podpod 100% 3 / 3 totaltotal 57.6% 117 / 203 CC 29 SCAR 19.1
CC
29
Cov
61%
_cache_has_all_cols
30.0
_gen_alternate_RecordCache_key
8.7
LoadByCols
6.4
CRAP 6.7
« SearchBuilder/Handle/Informix.pm ↑ Summary SearchBuilder/Union.pm »
1 package DBIx::SearchBuilder::Record::Cachable;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use DBIx::SearchBuilder::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Cache::Simple::TimedExpiry;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use base qw(DBIx::SearchBuilder::Record);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 DBIx::SearchBuilder::Record::Cachable - Records with caching behavior
14  
15 =head1 SYNOPSIS
16  
17 package MyRecord;
18 use base qw/DBIx::SearchBuilder::Record::Cachable/;
19  
20 =head1 DESCRIPTION
21  
22 This module subclasses the main L<DBIx::SearchBuilder::Record> package
23 to add a caching layer.
24  
25 The public interface remains the same, except that records which have
26 been loaded in the last few seconds may be reused by subsequent fetch
27 or load methods without retrieving them from the database.
28  
29 =head1 METHODS
30  
31 =cut
32  
33  
34 2 my %_CACHES = ();
35  
36 4 ▶sub _SetupCache {
Subroutine _SetupCache: called CC=1 SCAR=0.0
37 4 my ($self, $cache) = @_;
38 4 $_CACHES{$cache} = Cache::Simple::TimedExpiry->new();
39 4 $_CACHES{$cache}->expire_after( $self->_CacheConfig->{'cache_for_sec'} );
40 4 return $_CACHES{$cache};
41 }
42  
43  
44 =head2 FlushCache
45  
46 This class method flushes the _global_ DBIx::SearchBuilder::Record::Cachable
47 cache. All caches are immediately expired.
48  
49 =cut
50  
51 1 ▶sub FlushCache {
Subroutine FlushCache: called CC=1 SCAR=0.0
Pod documented
52 1 %_CACHES = ();
53 }
54  
55 =head2 _FlushKeyCache
56  
57 Blow away this record type's key cache
58  
59 =cut
60  
61 0 ▶sub _FlushKeyCache {
Subroutine _FlushKeyCache: not called CC=2 SCAR=17.9
62 0 my $self = shift;
63 0 ▶ my $cache = ($self->{_class}||= ref($self))."-KEYS";
Condition0%0/3
$self->{"_class"} ||= ref $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_class"} ||= ref $self
$self->{"_class"} ref $self
Truth table0%0/3
A ||= B
A= $self->{"_class"}B= ref $self
ABresult
1X1
011
000
64 0 return $self->_SetupCache($cache);
65 }
66  
67 6 ▶sub _KeyCache {
Subroutine _KeyCache: called CC=3 SCAR=11.3
68 6 my $self = shift;
69 6 ▶ my $cache = ($self->{_class}||= ref($self))."-KEYS";
Condition66%2/3
$self->{"_class"} ||= ref $self
l!l&&r!l&&!r
330
MC/DC0%0/2
$self->{"_class"} ||= ref $self
$self->{"_class"} ref $self
Truth table66%2/3
A ||= B
A= $self->{"_class"}B= ref $self
ABresult
1X1
011
000
70 6 ▶ return $_CACHES{$cache} || $self->_SetupCache($cache);
Condition66%2/3
$_CACHES{$cache} || $self->_SetupCache($cache)
l!l&&r!l&&!r
420
MC/DC0%0/2
$_CACHES{$cache} || $self->_SetupCache($cache)
$_CACHES{$cache} $self->_SetupCache($cache)
Truth table66%2/3
A || B
A= $_CACHES{$cache}B= $self->_SetupCache($cache)
ABresult
1X1
011
000
71 }
72  
73 6 ▶sub _RecordCache {
Subroutine _RecordCache: called CC=3 SCAR=12.0
74 6 my $self = shift;
75 6 ▶ my $cache = ($self->{_class}||= ref($self));
Condition33%1/3
$self->{"_class"} ||= ref $self
l!l&&r!l&&!r
600
MC/DC0%0/2
$self->{"_class"} ||= ref $self
$self->{"_class"} ref $self
Truth table33%1/3
A ||= B
A= $self->{"_class"}B= ref $self
ABresult
1X1
011
000
76 6 ▶ return $_CACHES{$cache} || $self->_SetupCache($cache);
Condition66%2/3
$_CACHES{$cache} || $self->_SetupCache($cache)
l!l&&r!l&&!r
420
MC/DC0%0/2
$_CACHES{$cache} || $self->_SetupCache($cache)
$_CACHES{$cache} $self->_SetupCache($cache)
Truth table66%2/3
A || B
A= $_CACHES{$cache}B= $self->_SetupCache($cache)
ABresult
1X1
011
000
77 }
78  
79 # Function: LoadFromHash
80 # Type : (overloaded) public instance
81 # Args : See DBIx::SearchBuilder::Record::LoadFromHash
82 # Lvalue : array(boolean, message)
83  
84 0 ▶sub LoadFromHash {
Subroutine LoadFromHash: not called CC=2 SCAR=17.9
Pod documented
85 0 my $self = shift;
86  
87 # Blow away the primary cache key since we're loading.
88 0 $self->{'_SB_Record_Primary_RecordCache_key'} = undef;
89 0 my ( $rvalue, $msg ) = $self->SUPER::LoadFromHash(@_);
90  
91 ## Check the return value, if its good, cache it!
92 0 ▶ $self->_store if $rvalue;
Branch: 1 branch
BranchTrueFalseTotal
if $rvalue 0 0 0
93  
94 0 return ( $rvalue, $msg );
95 }
96  
97 # Function: LoadByCols
98 # Type : (overloaded) public instance
99 # Args : see DBIx::SearchBuilder::Record::LoadByCols
100 # Lvalue : array(boolean, message)
101  
102 4 ▶sub LoadByCols {
Subroutine LoadByCols: called CC=6 SCAR=18.5
Pod documented
103 4 my ( $self, %attr ) = @_;
104  
105 # Blow away the primary cache key since we're loading.
106 4 $self->{'_SB_Record_Primary_RecordCache_key'} = undef;
107  
108 # generate the alternate cache key
109 4 my $alt_key = $self->_gen_alternate_RecordCache_key(%attr);
110 # get primary cache key
111 4 my $cache_key = $self->_lookup_primary_RecordCache_key($alt_key);
112 4 ▶ if ( $cache_key && $self->_fetch( $cache_key ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_key and $self->_fetch($cache_key)) 2 2 4
Condition66%2/3
$cache_key and $self->_fetch($cache_key)
!ll&&!rl&&r
022
MC/DC50%1/2
$cache_key and $self->_fetch($cache_key)
$cache_key $self->_fetch($cache_key)
Truth table66%2/3
A and B
A= $cache_keyB= $self->_fetch($cache_key)
ABresult
0X0
100
111
113  
114 # When SelectAllColumns is set, only return the cached entry if it
115 # already contains all real columns; otherwise fall through and
116 # re-fetch from the DB so the caller gets a complete record.
117 2 ▶ return ( 1, "Fetched from cache" ) if !$self->SelectAllColumns || $self->_cache_has_all_cols;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->SelectAllColumns or $self->_cache_has_all_cols 2 0 2
Condition33%1/3
not $self->SelectAllColumns or $self->_cache_has_all_cols
l!l&&r!l&&!r
200
MC/DC0%0/2
not $self->SelectAllColumns or $self->_cache_has_all_cols
not $self->SelectAllColumns $self->_cache_has_all_cols
Truth table33%1/3
not(A) or B
A= not $self->SelectAllColumnsB= $self->_cache_has_all_cols
ABresult
1X1
011
000
118 }
119  
120 # Fetch from the DB!
121 2 my ( $rvalue, $msg ) = $self->SUPER::LoadByCols(%attr);
122 # Check the return value, if its good, cache it!
123 2 ▶ if ($rvalue) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rvalue) 2 0 2
124 2 $self->_store();
125 # store alt_key as alias for pk
126 2 $self->_KeyCache->set( $alt_key, $self->_primary_RecordCache_key);
127 }
128 2 return ( $rvalue, $msg );
129 }
130  
131 # Returns true if every real column in _ClassAccessible has already been
132 # fetched into this record (i.e. the cache entry is complete enough to satisfy
133 # a SelectAllColumns request).
134 0 ▶sub _cache_has_all_cols {
Subroutine _cache_has_all_cols: not called CC=5 SCAR=34.0
135 0 my $self = shift;
136 0 my $ca = $self->_ClassAccessible;
137 0 for my $col ( keys %$ca ) {
138 0 ▶ next if $ca->{$col}{'foreign-collection'} || $ca->{$col}{'record-read'};
Branch: 1 branch
BranchTrueFalseTotal
if $ca->{$col}{"foreign-collection"} or $ca->{$col}{"record-read"} 0 0 0
Condition0%0/3
$ca->{$col}{"foreign-collection"} or $ca->{$col}{"record-read"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ca->{$col}{"foreign-collection"} or $ca->{$col}{"record-read"}
$ca->{$col}{"foreign-collection"} $ca->{$col}{"record-read"}
Truth table0%0/3
A or B
A= $ca->{$col}{"foreign-collection"}B= $ca->{$col}{"record-read"}
ABresult
1X1
011
000
139 0 ▶ return 0 unless $self->{'fetched'}{ lc $col };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"fetched"}{lc $col} 0 0 0
140 }
141 0 return 1;
142 }
143  
144 # Function: __Set
145 # Type : (overloaded) public instance
146 # Args : see DBIx::SearchBuilder::Record::_Set
147 # Lvalue : ?
148  
149 0 ▶sub __Set () {
Subroutine __Set: not called CC=1 SCAR=6.9
150 0 my $self = shift;
151  
152 0 $self->_expire;
153  
154 0 return $self->SUPER::__Set( @_ );
155 }
156  
157 # Function: Delete
158 # Type : (overloaded) public instance
159 # Args : nil
160 # Lvalue : ?
161  
162 0 ▶sub __Delete () {
Subroutine __Delete: not called CC=1 SCAR=6.9
163 0 my $self = shift;
164  
165 0 $self->_expire;
166  
167 0 return $self->SUPER::__Delete( @_ );
168  
169 }
170  
171 # Function: _expire
172 # Type : private instance
173 # Args : string(cache_key)
174 # Lvalue : 1
175 # Desc : Removes this object from the cache.
176  
177 0 ▶sub _expire (\$) {
Subroutine _expire: not called CC=2 SCAR=17.9
178 0 my $self = shift;
179 0 ▶ my $cache_key = $self->_primary_RecordCache_key or return;
Branch: 1 branch
BranchTrueFalseTotal
my $cache_key = $self->_primary_RecordCache_key or return 0 0 0
180 0 $self->_RecordCache->set( $cache_key, undef, time-1 );
181 # We should be doing something more surgical to clean out the
182 # key cache. but we do need to expire it
183 0 $self->_FlushKeyCache;
184 }
185  
186 # Function: _fetch
187 # Type : private instance
188 # Args : string(cache_key)
189 # Lvalue : 1
190 # Desc : Get an object from the cache, and make this object that.
191  
192 4 ▶sub _fetch () {
Subroutine _fetch: called CC=2 SCAR=6.9
193 4 my ( $self, $cache_key ) = @_;
194 4 ▶ my $data = $self->_RecordCache->fetch( $cache_key ) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $data = $self->_RecordCache->fetch($cache_key) or return 0 2 2 4
195 2 @{$self}{keys %$data} = values %$data; # deserialize
196 2 return 1;
197 }
198  
199  
200 # Function: _store
201 # Type : private instance
202 # Args : string(cache_key)
203 # Lvalue : 1
204 # Desc : Stores this object in the cache.
205  
206 2 ▶sub _store (\$) {
Subroutine _store: called CC=2 SCAR=7.0
207 2 my $self = shift;
208 2 ▶ my $key = $self->_primary_RecordCache_key or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $key = $self->_primary_RecordCache_key or return 0 2 0 2
209 2 $self->_RecordCache->set( $key, $self->_serialize );
210 2 return 1;
211 }
212  
213 2 ▶sub _serialize {
Subroutine _serialize: called CC=1 SCAR=0.0
214 2 my $self = shift;
215 return {
216 values => $self->{'values'},
217 table => $self->Table,
218 2 fetched => $self->{'fetched'}
219 };
220 }
221  
222 # Function: _gen_alternate_RecordCache_key
223 # Type : private instance
224 # Args : hash (attr)
225 # Lvalue : 1
226 # Desc : Takes a perl hash and generates a key from it.
227  
228 4 ▶sub _gen_alternate_RecordCache_key {
Subroutine _gen_alternate_RecordCache_key: called CC=6 SCAR=21.6
229 4 my ( $self, %attr ) = @_;
230 4 my $cache_key = '';
231 4 foreach my $key ( sort keys %attr ) {
232 4 my $value = $attr{$key};
233 4 ▶ unless ( defined $value ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $value) { } 4 0 4
if (do { ref $value eq 'HASH' }) { } 0 4 4
234 0 $value = '=__undef';
235 }
236 elsif ( ref($value) eq "HASH" ) {
237 $value = ( $value->{operator} || '=' )
238 0 ▶ . ( defined $value->{value}? $value->{value}: '__undef' );
Branch: 1 branch
BranchTrueFalseTotal
defined $value->{'value'} ? : 0 0 0
Condition0%0/2
$value->{'operator'} || '='
l!l
00
MC/DC0%0/1
$value->{'operator'} || '='
$value->{'operator'}
Truth table0%0/2
A
A= $value->{'operator'}
Aresult
11
00
239 }
240 else {
241 4 $value = "=" . $value;
242 }
243 4 $cache_key .= $key . $value . ',';
244 }
245 4 chop($cache_key);
246 4 return ($cache_key);
247 }
248  
249 # Function: _primary_RecordCache_key
250 # Type : private instance
251 # Args : none
252 # Lvalue: : 1
253 # Desc : generate a primary-key based variant of this object's cache key
254 # primary keys is in the cache
255  
256 4 ▶sub _primary_RecordCache_key {
Subroutine _primary_RecordCache_key: called CC=4 SCAR=13.9
257 4 my ($self) = @_;
258  
259 return $self->{'_SB_Record_Primary_RecordCache_key'}
260 4 ▶ if $self->{'_SB_Record_Primary_RecordCache_key'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_SB_Record_Primary_RecordCache_key"} 2 2 4
261  
262 2 my $cache_key = '';
263 2 my %pk = $self->PrimaryKeys;
264 2 foreach my $key ( sort keys %pk ) {
265 2 my $value = $pk{$key};
266 2 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 2 2
267 2 $cache_key .= $key . '=' . $value .',';
268 }
269 2 chop $cache_key;
270 2 return $self->{'_SB_Record_Primary_RecordCache_key'} = $cache_key;
271 }
272  
273 # Function: lookup_primary_RecordCache_key
274 # Type : private class
275 # Args : string(alternate cache id)
276 # Lvalue : string(cache id)
277  
278 4 ▶sub _lookup_primary_RecordCache_key {
Subroutine _lookup_primary_RecordCache_key: called CC=3 SCAR=11.4
279 4 my ($self, $key) = @_;
280 4 ▶ return undef unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 4 4
281 4 ▶ return $self->_KeyCache->fetch($key) || $key;
Condition66%2/3
$self->_KeyCache->fetch($key) || $key
l!l&&r!l&&!r
130
MC/DC0%0/2
$self->_KeyCache->fetch($key) || $key
$self->_KeyCache->fetch($key) $key
Truth table66%2/3
A || B
A= $self->_KeyCache->fetch($key)B= $key
ABresult
1X1
011
000
282 }
283  
284 =head2 _CacheConfig
285  
286 You can override this method to change the duration of the caching from the default of 5 seconds.
287  
288 For example, to cache records for up to 30 seconds, add the following method to your class:
289  
290 sub _CacheConfig {
291 { 'cache_for_sec' => 30 }
292 }
293  
294 =cut
295  
296 0 ▶sub _CacheConfig {
Subroutine _CacheConfig: not called CC=1 SCAR=6.9
297 return {
298 0 'cache_for_sec' => 5,
299 };
300 }
301  
302 2 1;
« SearchBuilder/Handle/Informix.pm ↑ Summary SearchBuilder/Union.pm »