| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Dancer::Plugin::Memcached; |
|
2
|
|
|
|
|
|
|
{ |
|
3
|
|
|
|
|
|
|
$Dancer::Plugin::Memcached::VERSION = '0.02'; |
|
4
|
|
|
|
|
|
|
} |
|
5
|
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
785
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
34
|
|
|
7
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
29
|
|
|
8
|
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
37
|
use 5.008; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
40
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
1119
|
use Dancer ':syntax'; |
|
|
1
|
|
|
|
|
382209
|
|
|
|
1
|
|
|
|
|
6
|
|
|
12
|
1
|
|
|
1
|
|
1216
|
use Dancer::Plugin; |
|
|
1
|
|
|
|
|
1378
|
|
|
|
1
|
|
|
|
|
83
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
1
|
|
|
1
|
|
953
|
use Cache::Memcached; |
|
|
1
|
|
|
|
|
91965
|
|
|
|
1
|
|
|
|
|
393
|
|
|
15
|
|
|
|
|
|
|
my $cache = Cache::Memcached->new; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 NAME |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Dancer::Plugin::Memcached - Cache response content to memcached |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
This plugin gives Dancer the ability to get and store page content in a memcached server, |
|
24
|
|
|
|
|
|
|
which in specific configurations could give a performance boost - particulary on GET requests |
|
25
|
|
|
|
|
|
|
that incur significant database calls. |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
In your configuration, a list of servers with port numbers needs to be defined. |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
plugins: |
|
30
|
|
|
|
|
|
|
Memcached: |
|
31
|
|
|
|
|
|
|
servers: |
|
32
|
|
|
|
|
|
|
- "10.0.0.15:11211" |
|
33
|
|
|
|
|
|
|
- "10.0.0.17:11211" |
|
34
|
|
|
|
|
|
|
default_timeout: 86400 |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
The C specifies an fallback time for keys to expire from the |
|
37
|
|
|
|
|
|
|
cache. If this value is less than 60*60*24*30 (30 days), time is assumed to be |
|
38
|
|
|
|
|
|
|
seconds after the key was stored. If larger, it's considered an absolute Unix time. |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
In your package: |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
package MyWebService; |
|
43
|
|
|
|
|
|
|
use Dancer; |
|
44
|
|
|
|
|
|
|
use Dancer::Plugin::Memcached; |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
get '/' sub => { |
|
47
|
|
|
|
|
|
|
# Do your logic here |
|
48
|
|
|
|
|
|
|
... |
|
49
|
|
|
|
|
|
|
memcached_set template($foo); |
|
50
|
|
|
|
|
|
|
}; |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
This plugin will use the PATH_INFO environment variable to store as the key so |
|
53
|
|
|
|
|
|
|
routes that make use of parameters in the form of "/foo/:bar" will be cached, |
|
54
|
|
|
|
|
|
|
but GET/POST variables will not. |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head1 KEYWORDS |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head2 memcached_check |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Will check for any route and return the page stored in memcached where available. |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=cut |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
register memcached_check => sub |
|
65
|
|
|
|
|
|
|
{ |
|
66
|
|
|
|
|
|
|
before sub |
|
67
|
|
|
|
|
|
|
{ |
|
68
|
0
|
|
|
0
|
|
|
my $set = plugin_setting; |
|
69
|
0
|
|
|
|
|
|
$cache->set_servers($set->{servers}); |
|
70
|
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
my $hit = $cache->get(request->{path_info}); |
|
72
|
0
|
0
|
|
|
|
|
return $hit if($hit); |
|
73
|
0
|
|
|
0
|
|
|
}; |
|
74
|
|
|
|
|
|
|
}; |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head2 memcached_set($content, [$expiration]) |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
For any given content, set and return the content. Expiration time for the set |
|
79
|
|
|
|
|
|
|
can optionally be set. |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=cut |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
register memcached_set => sub |
|
84
|
|
|
|
|
|
|
{ |
|
85
|
0
|
|
|
0
|
|
|
my($self, $content, $expiration) = plugin_args(@_); |
|
86
|
0
|
|
|
|
|
|
my $set = plugin_setting; |
|
87
|
0
|
|
|
|
|
|
$cache->set_servers($set->{servers}); |
|
88
|
|
|
|
|
|
|
|
|
89
|
0
|
|
0
|
|
|
|
my $hit = $cache->set( |
|
90
|
|
|
|
|
|
|
request->{path_info}, |
|
91
|
|
|
|
|
|
|
$content, |
|
92
|
|
|
|
|
|
|
$expiration || $set->{default_timeout} |
|
93
|
|
|
|
|
|
|
); |
|
94
|
|
|
|
|
|
|
|
|
95
|
0
|
0
|
|
|
|
|
return $content if $hit; |
|
96
|
|
|
|
|
|
|
}; |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 memcached_get($key) |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Grab a specified key. Returns false if the key is not found. |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=cut |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
register memcached_get => sub |
|
105
|
|
|
|
|
|
|
{ |
|
106
|
0
|
|
|
0
|
|
|
my ($self, $key) = plugin_args(@_); |
|
107
|
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
my $set = plugin_setting; |
|
109
|
0
|
|
|
|
|
|
$cache->set_servers($set->{servers}); |
|
110
|
|
|
|
|
|
|
|
|
111
|
0
|
|
|
|
|
|
return $cache->get($key); |
|
112
|
|
|
|
|
|
|
}; |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head2 memcached_store($key, $content, [$expiration]) |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
This keyword is identical to memcached_set with the exception that you can set |
|
117
|
|
|
|
|
|
|
any key name. |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=cut |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
register memcached_store => sub |
|
122
|
|
|
|
|
|
|
{ |
|
123
|
0
|
|
|
0
|
|
|
my($self, $key, $content, $expiration) = plugin_args(@_); |
|
124
|
0
|
|
|
|
|
|
my $set = plugin_setting; |
|
125
|
0
|
|
|
|
|
|
$cache->set_servers($set->{servers}); |
|
126
|
|
|
|
|
|
|
|
|
127
|
0
|
|
0
|
|
|
|
my $hit = $cache->set( |
|
128
|
|
|
|
|
|
|
$key, |
|
129
|
|
|
|
|
|
|
$content, |
|
130
|
|
|
|
|
|
|
$expiration || $set->{default_timeout} |
|
131
|
|
|
|
|
|
|
); |
|
132
|
|
|
|
|
|
|
|
|
133
|
0
|
0
|
|
|
|
|
return $content if $hit; |
|
134
|
|
|
|
|
|
|
}; |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
register_plugin; |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head1 AUTHOR |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Squeeks, C<< >> |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 CONTRIBUTORS |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Zefram, C<< >> |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 BUGS |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
|
150
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
|
151
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 SUPPORT |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
perldoc Dancer::Plugin::Memcached |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
You can also look for information at: |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=over 4 |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
L |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
L |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=item * CPAN Ratings |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
L |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=item * Search CPAN |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
L |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=back |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Dancer Web Framework - L |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Copyright 2013 Squeeks. |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
192
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
|
193
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=cut |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
1; |