blib/lib/Dancer/Plugin/Memcached.pm

filter: Statementstmt 57.7% 26 / 45 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 54.5% 6 / 11 Podpod n/a totaltotal 44.4% 32 / 72 CC 6 SCAR 17.7
CC
6
Cov
46%
__ANON__
12.0
__ANON__
12.0
__ANON__
6.0
CRAP 5.9
↑ Summary
1 package Dancer::Plugin::Memcached;
2 {
3 1 $Dancer::Plugin::Memcached::VERSION = '0.02';
4 }
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Cache::Memcached;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 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<default_timeout> 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 0 ▶{
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 before sub
67 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
68 0 my $set = plugin_setting;
69 0 $cache->set_servers($set->{servers});
70  
71 0 my $hit = $cache->get(request->{path_info});
72 0 ▶ return $hit if($hit);
Branch: 1 branch
BranchTrueFalseTotal
if $hit 0 0 0
73 0 };
74 1 };
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 0 ▶{
Subroutine __ANON__: not called CC=3 SCAR=24.8
85 0 my($self, $content, $expiration) = plugin_args(@_);
86 0 my $set = plugin_setting;
87 0 $cache->set_servers($set->{servers});
88  
89 my $hit = $cache->set(
90 request->{path_info},
91 $content,
92 $expiration || $set->{default_timeout}
93 0 ▶ );
Condition0%0/3
$expiration || $set->{'default_timeout'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$expiration || $set->{'default_timeout'}
$expiration $set->{'default_timeout'}
Truth table0%0/3
A || B
A= $expirationB= $set->{'default_timeout'}
ABresult
1X1
011
000
94  
95 0 ▶ return $content if $hit;
Branch: 1 branch
BranchTrueFalseTotal
if $hit 0 0 0
96 1 };
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 0 ▶{
Subroutine __ANON__: not called CC=1 SCAR=6.9
106 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 1 };
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 0 ▶{
Subroutine __ANON__: not called CC=3 SCAR=24.8
123 0 my($self, $key, $content, $expiration) = plugin_args(@_);
124 0 my $set = plugin_setting;
125 0 $cache->set_servers($set->{servers});
126  
127 my $hit = $cache->set(
128 $key,
129 $content,
130 $expiration || $set->{default_timeout}
131 0 ▶ );
Condition0%0/3
$expiration || $set->{'default_timeout'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$expiration || $set->{'default_timeout'}
$expiration $set->{'default_timeout'}
Truth table0%0/3
A || B
A= $expirationB= $set->{'default_timeout'}
ABresult
1X1
011
000
132  
133 0 ▶ return $content if $hit;
Branch: 1 branch
BranchTrueFalseTotal
if $hit 0 0 0
134 1 };
135  
136  
137 1 register_plugin;
138  
139 =head1 AUTHOR
140  
141 Squeeks, C<< <squeek at cpan.org> >>
142  
143 =head1 CONTRIBUTORS
144  
145 Zefram, C<< <zefram@fysh.org> >>
146  
147 =head1 BUGS
148  
149 Please report any bugs or feature requests to C<bug-dancer-plugin-memcached at rt.cpan.org>, or through
150 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dancer-Plugin-Memcached>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dancer-Plugin-Memcached>
167  
168 =item * AnnoCPAN: Annotated CPAN documentation
169  
170 L<http://annocpan.org/dist/Dancer-Plugin-Memcached>
171  
172 =item * CPAN Ratings
173  
174 L<http://cpanratings.perl.org/d/Dancer-Plugin-Memcached>
175  
176 =item * Search CPAN
177  
178 L<http://search.cpan.org/dist/Dancer-Plugin-Memcached/>
179  
180 =back
181  
182  
183 =head1 SEE ALSO
184  
185 Dancer Web Framework - L<Dancer>
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 1;
↑ Summary