| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package Class::DBI::Lite::CacheManager::Memcached; |
|
3
|
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
344
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
27
|
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings 'all'; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
38
|
|
|
6
|
1
|
|
|
1
|
|
5
|
use base 'Class::DBI::Lite::CacheManager'; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
288
|
|
|
7
|
1
|
|
|
1
|
|
284
|
use Cache::Memcached; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use Carp 'confess'; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub defaults |
|
12
|
|
|
|
|
|
|
{ |
|
13
|
|
|
|
|
|
|
return ( |
|
14
|
|
|
|
|
|
|
servers => undef, |
|
15
|
|
|
|
|
|
|
compress_threshold => 10_000, |
|
16
|
|
|
|
|
|
|
enable_compress => 0, |
|
17
|
|
|
|
|
|
|
debug => 0, |
|
18
|
|
|
|
|
|
|
lifetime => '30s', |
|
19
|
|
|
|
|
|
|
class => undef, |
|
20
|
|
|
|
|
|
|
); |
|
21
|
|
|
|
|
|
|
}# end defaults() |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# Public read-only properties: |
|
25
|
|
|
|
|
|
|
sub servers { shift->{servers} } |
|
26
|
|
|
|
|
|
|
sub compress_threshold { shift->{compress_threshold} } |
|
27
|
|
|
|
|
|
|
sub enable_compress { shift->{enable_compress} } |
|
28
|
|
|
|
|
|
|
sub debug { shift->{debug} } |
|
29
|
|
|
|
|
|
|
sub memd { shift->{class}->_meta->{memd} } |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub init |
|
32
|
|
|
|
|
|
|
{ |
|
33
|
|
|
|
|
|
|
my ($s) = @_; |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$s->{class}->_meta->{memd} = Cache::Memcached->new( |
|
36
|
|
|
|
|
|
|
servers => $s->servers, |
|
37
|
|
|
|
|
|
|
compress_threshold => $s->compress_threshold, |
|
38
|
|
|
|
|
|
|
enable_compress => $s->enable_compress, |
|
39
|
|
|
|
|
|
|
debug => $s->debug, |
|
40
|
|
|
|
|
|
|
); |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$s->{lifetime} ||= '30s'; |
|
43
|
|
|
|
|
|
|
my ($number, $unit) = $s->{lifetime} =~ m/^(\d+)([smhd])$/i; |
|
44
|
|
|
|
|
|
|
$unit = uc($unit); |
|
45
|
|
|
|
|
|
|
confess "Invalid lifetime value of '$s->{lifetime}'" unless $number && $unit; |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
my $expiry; |
|
48
|
|
|
|
|
|
|
if( $unit eq 'S' ) |
|
49
|
|
|
|
|
|
|
{ |
|
50
|
|
|
|
|
|
|
$expiry = $number; |
|
51
|
|
|
|
|
|
|
} |
|
52
|
|
|
|
|
|
|
elsif( $unit eq 'M' ) |
|
53
|
|
|
|
|
|
|
{ |
|
54
|
|
|
|
|
|
|
$expiry = $number * 60; |
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
elsif( $unit eq 'H' ) |
|
57
|
|
|
|
|
|
|
{ |
|
58
|
|
|
|
|
|
|
$expiry = $number * 60 * 60; |
|
59
|
|
|
|
|
|
|
} |
|
60
|
|
|
|
|
|
|
elsif( $unit eq 'D' ) |
|
61
|
|
|
|
|
|
|
{ |
|
62
|
|
|
|
|
|
|
$expiry = $number * 60 * 60 * 24; |
|
63
|
|
|
|
|
|
|
}# end if() |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
$s->{expiry} = $expiry; |
|
66
|
|
|
|
|
|
|
1; |
|
67
|
|
|
|
|
|
|
}# end init() |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub set |
|
71
|
|
|
|
|
|
|
{ |
|
72
|
|
|
|
|
|
|
my ($s, $key, $value) = @_; |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
$s->memd->set( $key, $value, $s->{expiry} ); |
|
75
|
|
|
|
|
|
|
}# end set() |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub get |
|
79
|
|
|
|
|
|
|
{ |
|
80
|
|
|
|
|
|
|
my ($s, $key) = @_; |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
$s->memd->get( $key ); |
|
83
|
|
|
|
|
|
|
}# end get() |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub delete |
|
87
|
|
|
|
|
|
|
{ |
|
88
|
|
|
|
|
|
|
my ($s, $key) = @_; |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
$s->memd->delete( $key ); |
|
91
|
|
|
|
|
|
|
}# end delete() |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub clear |
|
95
|
|
|
|
|
|
|
{ |
|
96
|
|
|
|
|
|
|
my ($s) = @_; |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
$s->memd->flush_all; |
|
99
|
|
|
|
|
|
|
}# end clear() |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
1;# return true: |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=pod |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 NAME |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Class::DBI::Lite::CacheManager::Memcached - Cache via memcached. |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
package app::user; |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
use strict; |
|
114
|
|
|
|
|
|
|
use warnings 'all'; |
|
115
|
|
|
|
|
|
|
use base 'app::model'; |
|
116
|
|
|
|
|
|
|
use Class::DBI::Lite::CacheManager::Memcached; |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
__PACKAGE__->set_up_table('users'); |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
__PACKAGE__->set_cache( |
|
121
|
|
|
|
|
|
|
Class::DBI::Lite::CacheManager::Memcached->new( |
|
122
|
|
|
|
|
|
|
lifetime => '30s', |
|
123
|
|
|
|
|
|
|
class => __PACKAGE__, |
|
124
|
|
|
|
|
|
|
servers => ['127.0.0.1:11211'], |
|
125
|
|
|
|
|
|
|
do_cache_search => 1, |
|
126
|
|
|
|
|
|
|
) |
|
127
|
|
|
|
|
|
|
); |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
__PACKAGE__->cache->cache_searches_containing(qw( |
|
130
|
|
|
|
|
|
|
email |
|
131
|
|
|
|
|
|
|
password |
|
132
|
|
|
|
|
|
|
)); |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Then, someplace else... |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# This will be cached... |
|
137
|
|
|
|
|
|
|
my ($user) = app::user->search( |
|
138
|
|
|
|
|
|
|
email => 'alice@wonderland.net', |
|
139
|
|
|
|
|
|
|
password => 'whiterabbit', |
|
140
|
|
|
|
|
|
|
); |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
...later... |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# This won't hit the database - the result will come from the cache instead: |
|
145
|
|
|
|
|
|
|
my ($user) = app::user->search( |
|
146
|
|
|
|
|
|
|
email => 'alice@wonderland.net', |
|
147
|
|
|
|
|
|
|
password => 'whiterabbit', |
|
148
|
|
|
|
|
|
|
); |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
A create, update or delete invalidates the cache: |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
$user->delete; # Cache is emptied now. |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
C uses L to temporarily |
|
157
|
|
|
|
|
|
|
store the results of (presumably) frequent database searches for faster lookup. |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
So, if your data requirements are such that you find objects of a specific class are getting called |
|
160
|
|
|
|
|
|
|
up frequently enough to warrant caching - you can now do that on a per-class basis. |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
You can even specify the kinds of search queries that should be cached. |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
You can specify the length of time that cached data should be available. |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
B More documentation and complete examples TBD. |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 AUTHOR |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Copyright John Drago . All rights reserved. |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 LICENSE |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
This software is B software and may be used and redistributed under the |
|
175
|
|
|
|
|
|
|
same terms as perl itself. |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=cut |
|
178
|
|
|
|
|
|
|
|