| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package Class::DBI::Lite::CacheManager; |
|
3
|
|
|
|
|
|
|
|
|
4
|
2
|
|
|
2
|
|
13
|
use strict; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
47
|
|
|
5
|
2
|
|
|
2
|
|
7
|
use warnings 'all'; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
45
|
|
|
6
|
2
|
|
|
2
|
|
9
|
use Carp 'confess'; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
86
|
|
|
7
|
2
|
|
|
2
|
|
10
|
use Digest::MD5 'md5_hex'; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
1483
|
|
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new |
|
10
|
|
|
|
|
|
|
{ |
|
11
|
1
|
|
|
1
|
0
|
341
|
my ($class, %args) = @_; |
|
12
|
|
|
|
|
|
|
|
|
13
|
1
|
|
|
|
|
4
|
my %defaults = ( |
|
14
|
|
|
|
|
|
|
__PACKAGE__->defaults, |
|
15
|
|
|
|
|
|
|
$class->defaults |
|
16
|
|
|
|
|
|
|
); |
|
17
|
1
|
|
|
|
|
6
|
my %params = ( |
|
18
|
|
|
|
|
|
|
%defaults, |
|
19
|
|
|
|
|
|
|
%args, |
|
20
|
|
|
|
|
|
|
); |
|
21
|
|
|
|
|
|
|
|
|
22
|
1
|
|
|
|
|
4
|
foreach my $arg ( keys %defaults ) |
|
23
|
|
|
|
|
|
|
{ |
|
24
|
|
|
|
|
|
|
confess "Required param '$arg' was not provided" |
|
25
|
6
|
50
|
|
|
|
13
|
unless defined( $params{$arg} ); |
|
26
|
|
|
|
|
|
|
}# end foreach() |
|
27
|
|
|
|
|
|
|
|
|
28
|
1
|
|
|
|
|
2
|
my $s = bless \%args, $class; |
|
29
|
1
|
|
|
|
|
5
|
$s->init(); |
|
30
|
1
|
|
|
|
|
6
|
$s->auto_setup(); |
|
31
|
1
|
|
|
|
|
9
|
return $s; |
|
32
|
|
|
|
|
|
|
}# end new() |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
0
|
0
|
|
sub init { } |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub defaults {( |
|
37
|
1
|
|
|
1
|
0
|
6
|
do_auto_setup => 1, |
|
38
|
|
|
|
|
|
|
do_cache_retrieve => 1, |
|
39
|
|
|
|
|
|
|
do_cache_search => 0, |
|
40
|
|
|
|
|
|
|
search_options => [ ], |
|
41
|
|
|
|
|
|
|
class => undef |
|
42
|
|
|
|
|
|
|
)} |
|
43
|
|
|
|
|
|
|
|
|
44
|
0
|
|
|
0
|
0
|
0
|
sub do_auto_setup { shift->{do_auto_setup} } |
|
45
|
|
|
|
|
|
|
|
|
46
|
1
|
|
|
1
|
0
|
3
|
sub do_cache_retrieve { shift->{do_cache_retrieve} } |
|
47
|
|
|
|
|
|
|
|
|
48
|
1
|
|
|
1
|
0
|
3
|
sub do_cache_search { shift->{do_cache_search} } |
|
49
|
|
|
|
|
|
|
|
|
50
|
100040
|
|
|
100040
|
0
|
116312
|
sub search_options { @{ shift->{search_options} } } |
|
|
100040
|
|
|
|
|
172503
|
|
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub cache_searches_containing |
|
53
|
|
|
|
|
|
|
{ |
|
54
|
1
|
|
|
1
|
0
|
4
|
my ($s, @cols) = @_; |
|
55
|
|
|
|
|
|
|
|
|
56
|
1
|
|
|
|
|
7
|
my $sig = md5_hex( join ':', sort @cols ); |
|
57
|
1
|
|
|
|
|
3
|
push @{$s->{search_options}}, $sig; |
|
|
1
|
|
|
|
|
4
|
|
|
58
|
|
|
|
|
|
|
}# end cache_searches_containing() |
|
59
|
|
|
|
|
|
|
|
|
60
|
0
|
|
|
0
|
0
|
0
|
sub class { shift->{class} } |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub set; |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub get; |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub delete; |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub clear; |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub auto_setup |
|
71
|
|
|
|
|
|
|
{ |
|
72
|
1
|
|
|
1
|
0
|
1
|
my $s = shift; |
|
73
|
|
|
|
|
|
|
|
|
74
|
1
|
|
|
|
|
4
|
my $class = $s->class; |
|
75
|
|
|
|
|
|
|
|
|
76
|
1
|
50
|
|
|
|
4
|
if( $s->do_cache_retrieve ) |
|
77
|
|
|
|
|
|
|
{ |
|
78
|
|
|
|
|
|
|
$class->add_trigger( before_retrieve => sub { |
|
79
|
0
|
|
|
0
|
|
0
|
my ($s, $id) = @_; |
|
80
|
0
|
|
|
|
|
0
|
my $key = $s->get_cache_key( $id ); |
|
81
|
0
|
|
|
|
|
0
|
$class->cache->get( $key ); |
|
82
|
0
|
|
|
|
|
0
|
}); |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
$class->add_trigger( after_retrieve => sub { |
|
85
|
0
|
|
|
0
|
|
0
|
my $s = shift; |
|
86
|
0
|
|
|
|
|
0
|
$class->cache->set( $s->get_cache_key => $s->as_hashref ); |
|
87
|
0
|
|
|
|
|
0
|
}); |
|
88
|
|
|
|
|
|
|
}# end if() |
|
89
|
|
|
|
|
|
|
|
|
90
|
1
|
50
|
|
|
|
7
|
if( $s->do_cache_search ) |
|
91
|
|
|
|
|
|
|
{ |
|
92
|
|
|
|
|
|
|
$class->add_trigger( before_search => sub { |
|
93
|
100000
|
|
|
100000
|
|
147388
|
my ($s, $params) = @_; |
|
94
|
|
|
|
|
|
|
|
|
95
|
100000
|
|
|
|
|
353825
|
my $sig = md5_hex(join ':', sort keys %$params); |
|
96
|
100000
|
50
|
|
|
|
226389
|
return unless grep { $_ eq $sig } ( $s->cache->search_options ); |
|
|
100000
|
|
|
|
|
251738
|
|
|
97
|
|
|
|
|
|
|
|
|
98
|
100000
|
|
|
|
|
183525
|
my $id = md5_hex( join ':', map { "$_=$params->{$_}" } sort keys %$params ); |
|
|
100000
|
|
|
|
|
327942
|
|
|
99
|
100000
|
|
|
|
|
217640
|
my $key = $s->get_cache_key( $id ); |
|
100
|
|
|
|
|
|
|
|
|
101
|
100000
|
100
|
|
|
|
196666
|
my $cached = $class->cache->get( $key ) |
|
102
|
|
|
|
|
|
|
or return; |
|
103
|
|
|
|
|
|
|
|
|
104
|
99960
|
|
|
|
|
126844
|
my @res = grep { $_ } @{ $cached->{data} }; |
|
|
99960
|
|
|
|
|
183875
|
|
|
|
99960
|
|
|
|
|
162285
|
|
|
105
|
99960
|
50
|
|
|
|
204875
|
return unless @res; |
|
106
|
99960
|
|
|
|
|
194437
|
@res; |
|
107
|
1
|
|
|
|
|
7
|
}); |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
$class->add_trigger( after_search => sub { |
|
110
|
40
|
|
|
40
|
|
73
|
my ($s, $params, $result_array) = @_; |
|
111
|
|
|
|
|
|
|
|
|
112
|
40
|
|
|
|
|
205
|
my $sig = md5_hex(join ':', sort keys %$params); |
|
113
|
40
|
50
|
|
|
|
107
|
return unless grep { $_ eq $sig } ( $s->cache->search_options ); |
|
|
40
|
|
|
|
|
120
|
|
|
114
|
|
|
|
|
|
|
|
|
115
|
40
|
|
|
|
|
87
|
my $id = md5_hex( join ':', map { "$_=$params->{$_}" } sort keys %$params ); |
|
|
40
|
|
|
|
|
142
|
|
|
116
|
40
|
|
|
|
|
108
|
my $key = $s->get_cache_key( $id ); |
|
117
|
|
|
|
|
|
|
|
|
118
|
40
|
|
|
|
|
73
|
my @objects = map { $_->as_hashref } @$result_array; |
|
|
40
|
|
|
|
|
85
|
|
|
119
|
40
|
|
|
|
|
98
|
$class->cache->set( $key => { data => \@objects } ); |
|
120
|
1
|
|
|
|
|
5
|
}); |
|
121
|
|
|
|
|
|
|
}# end if() |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
$class->add_trigger( after_create => sub { |
|
124
|
10
|
|
|
10
|
|
17
|
my $s = shift; |
|
125
|
10
|
|
|
|
|
44
|
$class->cache->clear(); |
|
126
|
1
|
|
|
|
|
10
|
}); |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
$class->add_trigger( after_update => sub { |
|
129
|
0
|
|
|
0
|
|
0
|
my $s = shift; |
|
130
|
0
|
|
|
|
|
0
|
$class->cache->clear(); |
|
131
|
1
|
|
|
|
|
5
|
}); |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
$class->add_trigger( after_delete => sub { |
|
134
|
11
|
|
|
11
|
|
89
|
my $s = shift; |
|
135
|
11
|
|
|
|
|
78
|
$class->cache->clear(); |
|
136
|
1
|
|
|
|
|
5
|
}); |
|
137
|
|
|
|
|
|
|
}# end auto_setup() |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
1;# return true: |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=pod |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 NAME |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Class::DBI::Lite::CacheManager - Base class for NoSQL cache managers. |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
You should not use this class directly - use L |
|
150
|
|
|
|
|
|
|
or L. |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
B "NoSQL" is "Not Only SQL" - not "No SQL". |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Many - but not all - database queries can be avoided by using a simple cache system. |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
The CacheManager extentions for L offer the following features: |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=over 4 |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=item * B |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item * Per-class caching options - specify different cache parameters on a per-class basis. |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=item * Reduced load on the database. |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item * Reduced network traffic. |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=back |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
L and L for |
|
175
|
|
|
|
|
|
|
implementation-specific details. |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head1 AUTHOR |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Copyright John Drago . All rights reserved. |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head1 LICENSE |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
This software is B software and may be used and redistributed under the |
|
184
|
|
|
|
|
|
|
same terms as perl itself. |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=cut |
|
187
|
|
|
|
|
|
|
|