lib/Class/DBI/Lite/CacheManager/InMemory.pm

filter: Statementstmt 87.1% 34 / 39 Branchbran 33.3% 4 / 12 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 7 totaltotal 67.0% 55 / 82 CC 10 SCAR 19.5
CC
10
Cov
73%
init
15.2
get
3.0
delete
2.0
CRAP 7.0
« lib/Class/DBI/Lite/Pager.pm ↑ Summary lib/Class/DBI/Lite/SQLite.pm »
1  
2 package Class::DBI::Lite::CacheManager::InMemory;
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base 'Class::DBI::Lite::CacheManager';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 sub defaults
11 1 ▶{
Subroutine defaults: called CC=1 SCAR=0.0
Pod undocumented
12 return (
13 1 lifetime => '30s',
14 class => undef,
15 );
16 }# end defaults()
17  
18  
19 1 ▶sub class { shift->{class} }
Subroutine class: called CC=1 SCAR=0.0
Pod undocumented
20  
21  
22 sub init
23 1 ▶{
Subroutine init: called CC=8 SCAR=27.2
Pod undocumented
24 1 my ($s) = @_;
25
26 1 ▶ $s->{lifetime} ||= '30s';
Condition50%1/2
$s->{"lifetime"} ||= "30s"
l!l
10
MC/DC0%0/1
$s->{"lifetime"} ||= "30s"
$s->{"lifetime"}
Truth table50%1/2
A
A= $s->{"lifetime"}
Aresult
11
00
27 1 my ($number, $unit) = $s->{lifetime} =~ m/^(\d+)([smhd])$/i;
28 1 $unit = uc($unit);
29 1 ▶ confess "Invalid lifetime value of '$s->{lifetime}'" unless $number && $unit;
Branch: 1 branch
BranchTrueFalseTotal
unless $number and $unit 0 1 1
Condition33%1/3
$number and $unit
!ll&&!rl&&r
001
MC/DC0%0/2
$number and $unit
$number $unit
Truth table33%1/3
A and B
A= $numberB= $unit
ABresult
0X0
100
111
30
31 1 my $expiry;
32 1 ▶ if( $unit eq 'S' )
Branch: 4 branches
BranchTrueFalseTotal
if ($unit eq 'S') { } 1 0 1
elsif ($unit eq 'M') { } 0 0 0
elsif ($unit eq 'H') { } 0 0 0
elsif ($unit eq 'D') { } 0 0 0
33 {
34 1 $expiry = $number;
35 }
36 elsif( $unit eq 'M' )
37 {
38 0 $expiry = $number * 60;
39 }
40 elsif( $unit eq 'H' )
41 {
42 0 $expiry = $number * 60 * 60;
43 }
44 elsif( $unit eq 'D' )
45 {
46 0 $expiry = $number * 60 * 60 * 24;
47 }# end if()
48
49 1 $s->{expiry} = $expiry;
50 1 $s->{cache} = { };
51 1 1;
52 }# end init()
53  
54  
55 sub set
56 20 ▶{
Subroutine set: called CC=1 SCAR=0.0
Pod undocumented
57 20 my ($s, $key, $value) = @_;
58
59 20 my $exp = time() + $s->{expiry};
60 20 $s->{cache}{$key} = { expires => $exp, value => $value };
61 }# end set()
62  
63  
64 sub get
65 100000 ▶{
Subroutine get: called CC=3 SCAR=11.0
Pod undocumented
66 100000 my ($s, $key) = @_;
67  
68 100000 ▶ return unless exists($s->{cache}{$key}) && $s->{cache}{$key}->{expires} > time();
Branch: 1 branch
BranchTrueFalseTotal
unless exists $s->{"cache"}{$key} and $s->{"cache"}{$key}{"expires"} > time 20 99980 100000
Condition100%3/3
exists $s->{"cache"}{$key} and $s->{"cache"}{$key}{"expires"} > time
!ll&&!rl&&r
101099980
MC/DC100%2/2
exists $s->{"cache"}{$key} and $s->{"cache"}{$key}{"expires"} > time
exists $s->{"cache"}{$key} $s->{"cache"}{$key}{"expires"} > time
Truth table100%3/3
A and B
A= exists $s->{"cache"}{$key}B= $s->{"cache"}{$key}{"expires"} > time
ABresult
0X0
100
111
69 99980 return $s->{cache}{$key}->{value};
70 }# end get()
71  
72  
73 sub delete
74 0 ▶{
Subroutine delete: not called CC=1 SCAR=6.9
Pod undocumented
75 0 my ($s, $key) = @_;
76  
77 0 delete( $s->{cache}{$key} );
78 }# end delete()
79  
80  
81 sub clear
82 22 ▶{
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
83 22 my ($s) = @_;
84
85 22 $s->{cache} = { };
86 }# end clear()
87  
88 1 1;# return true:
89  
90 =pod
91  
92 =head1 NAME
93  
94 Class::DBI::Lite::CacheManager::InMemory - Cache in RAM.
95  
96 =head1 SYNOPSIS
97  
98 package app::user;
99
100 use strict;
101 use warnings 'all';
102 use base 'app::model';
103 use Class::DBI::Lite::CacheManager::InMemory;
104
105 __PACKAGE__->set_up_table('users');
106
107 __PACKAGE__->set_cache(
108 Class::DBI::Lite::CacheManager::Memcached->new(
109 lifetime => '30s',
110 class => __PACKAGE__,
111 do_cache_search => 1,
112 )
113 );
114
115 __PACKAGE__->cache->cache_searches_containing(qw(
116 email
117 password
118 ));
119  
120 Then, someplace else...
121  
122 # This will be cached...
123 my ($user) = app::user->search(
124 email => 'alice@wonderland.net',
125 password => 'whiterabbit',
126 );
127  
128 ...later - within 30 seconds...
129  
130 # This won't hit the database - the result will come from the cache instead:
131 my ($user) = app::user->search(
132 email => 'alice@wonderland.net',
133 password => 'whiterabbit',
134 );
135  
136 A create, update or delete invalidates the cache:
137  
138 $user->delete; # Cache is emptied now.
139  
140 =head1 DESCRIPTION
141  
142 C<Class::DBI::Lite::CacheManager::InMemory> will store the results of searches
143 in RAM for a specific length of time. This is helpful if you find that your
144 application's performance is suffering because of oft-repeated queries.
145  
146 So, if your data requirements are such that you find objects of a specific class are getting called
147 up frequently enough to warrant caching - you can now do that on a per-class basis.
148  
149 You can even specify the kinds of search queries that should be cached.
150  
151 You can specify the length of time that cached data should be available.
152  
153 B<NOTE:> More documentation and complete examples TBD.
154  
155 =head1 AUTHOR
156  
157 Copyright John Drago <jdrago_999@yahoo.com>. All rights reserved.
158  
159 =head1 LICENSE
160  
161 This software is B<Free> software and may be used and redistributed under the
162 same terms as perl itself.
163  
164 =cut
165  
« lib/Class/DBI/Lite/Pager.pm ↑ Summary lib/Class/DBI/Lite/SQLite.pm »