line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package Class::DBI::Lite::CacheManager::InMemory; |
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
414
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings 'all'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
6
|
1
|
|
|
1
|
|
4
|
use base 'Class::DBI::Lite::CacheManager'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
294
|
|
7
|
1
|
|
|
1
|
|
6
|
use Carp 'confess'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
324
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub defaults |
11
|
|
|
|
|
|
|
{ |
12
|
|
|
|
|
|
|
return ( |
13
|
1
|
|
|
1
|
0
|
5
|
lifetime => '30s', |
14
|
|
|
|
|
|
|
class => undef, |
15
|
|
|
|
|
|
|
); |
16
|
|
|
|
|
|
|
}# end defaults() |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
1
|
0
|
3
|
sub class { shift->{class} } |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub init |
23
|
|
|
|
|
|
|
{ |
24
|
1
|
|
|
1
|
0
|
2
|
my ($s) = @_; |
25
|
|
|
|
|
|
|
|
26
|
1
|
|
50
|
|
|
5
|
$s->{lifetime} ||= '30s'; |
27
|
1
|
|
|
|
|
6
|
my ($number, $unit) = $s->{lifetime} =~ m/^(\d+)([smhd])$/i; |
28
|
1
|
|
|
|
|
3
|
$unit = uc($unit); |
29
|
1
|
50
|
33
|
|
|
7
|
confess "Invalid lifetime value of '$s->{lifetime}'" unless $number && $unit; |
30
|
|
|
|
|
|
|
|
31
|
1
|
|
|
|
|
1
|
my $expiry; |
32
|
1
|
50
|
|
|
|
9
|
if( $unit eq 'S' ) |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
33
|
|
|
|
|
|
|
{ |
34
|
1
|
|
|
|
|
3
|
$expiry = $number; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
elsif( $unit eq 'M' ) |
37
|
|
|
|
|
|
|
{ |
38
|
0
|
|
|
|
|
0
|
$expiry = $number * 60; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
elsif( $unit eq 'H' ) |
41
|
|
|
|
|
|
|
{ |
42
|
0
|
|
|
|
|
0
|
$expiry = $number * 60 * 60; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
elsif( $unit eq 'D' ) |
45
|
|
|
|
|
|
|
{ |
46
|
0
|
|
|
|
|
0
|
$expiry = $number * 60 * 60 * 24; |
47
|
|
|
|
|
|
|
}# end if() |
48
|
|
|
|
|
|
|
|
49
|
1
|
|
|
|
|
2
|
$s->{expiry} = $expiry; |
50
|
1
|
|
|
|
|
3
|
$s->{cache} = { }; |
51
|
1
|
|
|
|
|
2
|
1; |
52
|
|
|
|
|
|
|
}# end init() |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub set |
56
|
|
|
|
|
|
|
{ |
57
|
40
|
|
|
40
|
0
|
76
|
my ($s, $key, $value) = @_; |
58
|
|
|
|
|
|
|
|
59
|
40
|
|
|
|
|
87
|
my $exp = time() + $s->{expiry}; |
60
|
40
|
|
|
|
|
185
|
$s->{cache}{$key} = { expires => $exp, value => $value }; |
61
|
|
|
|
|
|
|
}# end set() |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub get |
65
|
|
|
|
|
|
|
{ |
66
|
100000
|
|
|
100000
|
0
|
155209
|
my ($s, $key) = @_; |
67
|
|
|
|
|
|
|
|
68
|
100000
|
100
|
100
|
|
|
438543
|
return unless exists($s->{cache}{$key}) && $s->{cache}{$key}->{expires} > time(); |
69
|
99960
|
|
|
|
|
266011
|
return $s->{cache}{$key}->{value}; |
70
|
|
|
|
|
|
|
}# end get() |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub delete |
74
|
|
|
|
|
|
|
{ |
75
|
0
|
|
|
0
|
0
|
0
|
my ($s, $key) = @_; |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
0
|
delete( $s->{cache}{$key} ); |
78
|
|
|
|
|
|
|
}# end delete() |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub clear |
82
|
|
|
|
|
|
|
{ |
83
|
22
|
|
|
22
|
0
|
60
|
my ($s) = @_; |
84
|
|
|
|
|
|
|
|
85
|
22
|
|
|
|
|
108
|
$s->{cache} = { }; |
86
|
|
|
|
|
|
|
}# end clear() |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
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 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 More documentation and complete examples TBD. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 AUTHOR |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Copyright John Drago . All rights reserved. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 LICENSE |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
This software is B software and may be used and redistributed under the |
162
|
|
|
|
|
|
|
same terms as perl itself. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=cut |
165
|
|
|
|
|
|
|
|