Cache.pm

filter: Statementstmt 55.1% 54 / 98 Branchbran 28.5% 8 / 28 Conditioncond 43.4% 10 / 23 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 56.5% 13 / 23 Podpod 100% 1 / 1 totaltotal 46.4% 86 / 185 CC 24 SCAR 18.3
CC
24
Cov
48%
_cache_start_playback
20.0
_cache_start_playback_or_record
20.0
_cache_get_tx
6.0
CRAP 6.2
↑ Summary Cache/Driver/File.pm »
1 package Mojo::UserAgent::Role::Cache;
2 5 ▶use Mojo::Base -role;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 5 ▶use Mojo::UserAgent::Role::Cache::Driver::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Mojo::Util 'term_escape';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use constant DEBUG => $ENV{MOJO_CLIENT_DEBUG} || $ENV{MOJO_UA_CACHE_DEBUG} || 0;
Subroutine BEGIN: called CC=3 SCAR=12.5
Condition40%2/5
$ENV{'MOJO_CLIENT_DEBUG'} || $ENV{'MOJO_UA_CACHE_DEBUG'} || 0
l!l
05
$ENV{"MOJO_CLIENT_DEBUG"} or $ENV{"MOJO_UA_CACHE_DEBUG"}
l!l&&r!l&&!r
005
MC/DC0%0/2
$ENV{'MOJO_CLIENT_DEBUG'} || $ENV{'MOJO_UA_CACHE_DEBUG'} || 0
$ENV{"MOJO_CLIENT_DEBUG"} $ENV{"MOJO_UA_CACHE_DEBUG"}
Truth table33%1/3
A or B
A= $ENV{"MOJO_CLIENT_DEBUG"}B= $ENV{"MOJO_UA_CACHE_DEBUG"}
ABresult
1X1
011
000
8 5 ▶use constant TRACE => $ENV{MOJO_CLIENT_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MOJO_CLIENT_DEBUG'} || 0
l!l
05
MC/DC0%0/1
$ENV{'MOJO_CLIENT_DEBUG'} || 0
$ENV{'MOJO_CLIENT_DEBUG'}
Truth table50%1/2
A
A= $ENV{'MOJO_CLIENT_DEBUG'}
Aresult
11
00
9  
10 5 our $VERSION = '0.03';
11  
12 5 my $DEFAULT_STRATEGY = 'playback_or_record';
13  
14 3 ▶has cache_driver => sub { shift->cache_driver_singleton };
Subroutine __ANON__: called CC=1 SCAR=0.0
15  
16 3 ▶has cache_key => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
17 62 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
18 62 my $req = shift->req;
19 62 my $url = $req->url;
20 62 ▶ my @key = (lc $req->method, $url->host || 'local', map { _escape($_) } @{$url->path});
Condition50%1/2
$url->host || 'local'
l!l
620
MC/DC0%0/1
$url->host || 'local'
$url->host
Truth table50%1/2
A
A= $url->host
Aresult
11
00
21  
22 # The question marks is a hack to make sure the path parts will
23 # never be in conflict with the query and body.
24 62 push @key, '?q=' . $url->query->to_string;
25 62 ▶ push @key, '?b=' . Mojo::Util::md5_sum($req->body) if length $req->body // '';
Branch: 1 branch
BranchTrueFalseTotal
if length $req->body // "" 2 60 62
Condition50%1/2
length $req->body // ""
l!l
620
MC/DC0%0/1
length $req->body // ""
length $req->body
Truth table50%1/2
A
A= length $req->body
Aresult
11
00
26  
27 62 return \@key;
28 3 };
29 5 };
30  
31 2 ▶has cache_strategy => sub {
Subroutine __ANON__: called CC=4 SCAR=15.9
32 2 ▶ my $strategy = $ENV{MOJO_USERAGENT_CACHE_STRATEGY} || $DEFAULT_STRATEGY;
Condition33%1/3
$ENV{'MOJO_USERAGENT_CACHE_STRATEGY'} || $DEFAULT_STRATEGY
l!l&&r!l&&!r
200
MC/DC0%0/2
$ENV{'MOJO_USERAGENT_CACHE_STRATEGY'} || $DEFAULT_STRATEGY
$ENV{'MOJO_USERAGENT_CACHE_STRATEGY'} $DEFAULT_STRATEGY
Truth table33%1/3
A || B
A= $ENV{'MOJO_USERAGENT_CACHE_STRATEGY'}B= $DEFAULT_STRATEGY
ABresult
1X1
011
000
33 2 my @strategies = map { split /=/, $_, 2 } split '&', $strategy;
34 2 ▶ my %strategies = @strategies == 1 ? () : @strategies;
Branch: 1 branch
BranchTrueFalseTotal
@strategies == 1 ? : 0 2 2
35  
36 0 ▶ return !%strategies ? sub {$strategy} : sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: called CC=3 SCAR=13.3
37 4 my $method = uc shift->req->method;
38 4 ▶ return $strategies{$method} || $strategies{DEFAULT} || $DEFAULT_STRATEGY;
Condition50%3/6
$strategies{$method} || $strategies{'DEFAULT'} || $DEFAULT_STRATEGY
l!l&&r!l&&!r
400
$strategies{$method} or $strategies{"DEFAULT"}
l!l&&r!l&&!r
220
MC/DC0%0/3
$strategies{$method} || $strategies{'DEFAULT'} || $DEFAULT_STRATEGY
$strategies{$method} $strategies{"DEFAULT"} $DEFAULT_STRATEGY
Truth table50%2/4
A or B || C
A= $strategies{$method}B= $strategies{"DEFAULT"}C= $DEFAULT_STRATEGY
ABCresult
1XX1
01X1
0011
0000
39 2 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
!%strategies ? : 0 2 2
40 5 };
41  
42 7 ▶sub cache_driver_singleton {
Subroutine cache_driver_singleton: called CC=2 SCAR=6.9
Pod documented
43 7 my $class = shift;
44 7 state $driver = Mojo::UserAgent::Role::Cache::Driver::File->new;
45 7 ▶ return $driver unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 6 1 7
46 1 $driver = shift;
47 1 return $class;
48 }
49  
50 1 ▶around start => sub {
Subroutine __ANON__: called CC=4 SCAR=15.5
51 1 my ($orig, $self, $tx) = (shift, shift, shift);
52  
53 1 my $strategy = $self->cache_strategy->($tx);
54 1 warn qq(-- Cache strategy is "$strategy" (@{[_url($tx)]})\n) if DEBUG and !$self->{cache_passthrough};
55 1 ▶ return $self->$orig($tx, @_) if $strategy eq 'passthrough' or delete $self->{cache_passthrough};
Branch: 1 branch
BranchTrueFalseTotal
if $strategy eq "passthrough" or delete $self->{"cache_passthrough"} 0 1 1
Condition33%1/3
$strategy eq "passthrough" or delete $self->{"cache_passthrough"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$strategy eq "passthrough" or delete $self->{"cache_passthrough"}
$strategy eq "passthrough" delete $self->{"cache_passthrough"}
Truth table33%1/3
A or B
A= $strategy eq "passthrough"B= delete $self->{"cache_passthrough"}
ABresult
1X1
011
000
56  
57 1 my $method = $self->can("_cache_start_$strategy");
58 1 ▶ Carp::confess(qq([Mojo::UserAgent::Role::Cache] Invalid strategy "$strategy".)) unless $method;
Branch: 1 branch
BranchTrueFalseTotal
unless $method 1 0 1
59 0 return $self->$method($tx, @_);
60 5 };
61  
62 54 ▶sub _escape {
Subroutine _escape: called CC=1 SCAR=0.0
63 54 local $_ = Mojo::Util::url_escape($_[0]);
64 54 s!_!%5F!g;
65 54 s!%!_!g;
66 54 $_;
67 }
68  
69 0 ▶sub _url { shift->req->url->to_abs }
Subroutine _url: not called CC=1 SCAR=6.9
70  
71 0 ▶sub _cache_get_tx {
Subroutine _cache_get_tx: not called CC=2 SCAR=17.9
72 0 my ($self, $tx_input) = @_;
73 0 my $key = $self->cache_key->($tx_input);
74  
75 0 my $buffer = $self->cache_driver->get($key);
76 0 ▶ return undef unless defined $buffer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $buffer 0 0 0
77  
78 0 $tx_input->res->parse($buffer);
79 0 return $tx_input;
80 }
81  
82 0 ▶sub _cache_set_tx {
Subroutine _cache_set_tx: not called CC=1 SCAR=6.9
83 0 my ($self, $tx_input, $tx_output) = @_;
84 0 $self->cache_driver->set($self->cache_key->($tx_input), $tx_output->res->to_string);
85 0 return $self;
86 }
87  
88 0 ▶sub _cache_start_playback {
Subroutine _cache_start_playback: not called CC=4 SCAR=30.0
89 0 my ($self, $tx_input, $cb) = @_;
90 0 my $tx_output = $self->_cache_get_tx($tx_input);
91 0 ▶ my $status = $tx_output ? '<<<' : '!!!';
Branch: 1 branch
BranchTrueFalseTotal
$tx_output ? : 0 0 0
92  
93 # Not in cache
94 0 ▶ unless ($tx_output) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tx_output) 0 0 0
95 0 $tx_output = $tx_input;
96 0 $tx_output->res->error({message => 'Not in cache.'});
97 }
98  
99 0 warn term_escape "-- Client >>> Cache (@{[_url($tx_input)]})\n@{[$tx_input->req->to_string]}\n" if TRACE;
100 0 warn term_escape "-- Client $status Cache (@{[_url($tx_input)]})\n@{[$tx_output->res->to_string]}\n" if TRACE;
101  
102 # Blocking
103 0 ▶ return $tx_output unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 0 0 0
104  
105 # Non-blocking
106 0 ▶ Mojo::IOLoop->next_tick(sub { $self->$cb($tx_input) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
107 0 return $self;
108 }
109  
110 0 ▶sub _cache_start_playback_or_record {
Subroutine _cache_start_playback_or_record: not called CC=4 SCAR=30.0
111 0 my ($self, $tx_input, $cb) = @_;
112 0 my $tx_output = $self->_cache_get_tx($tx_input);
113  
114 # Not cached
115 0 ▶ unless ($tx_output) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tx_output) 0 0 0
116 0 warn term_escape "-- Client !!! Cache (@{[_url($tx_input)]}) - Start recording...\n" if DEBUG;
117 0 ▶ return $self->_cache_start_record($tx_input, $cb ? ($cb) : ());
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 0 0 0
118 }
119  
120 0 warn term_escape "-- Client >>> Cache (@{[_url($tx_input)]})\n@{[$tx_input->req->to_string]}\n" if TRACE;
121 0 warn term_escape "-- Client <<< Cache (@{[_url($tx_input)]})\n@{[$tx_output->res->to_string]}\n" if TRACE;
122  
123 # Blocking
124 0 ▶ return $tx_output unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 0 0 0
125  
126 # Non-blocking
127 0 ▶ Mojo::IOLoop->next_tick(sub { $self->$cb($tx_output) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 0 return $self;
129 }
130  
131 0 ▶sub _cache_start_record {
Subroutine _cache_start_record: not called CC=2 SCAR=17.9
132 0 my ($self, $tx_input, $cb) = @_;
133  
134 # Make sure we perform the actual request when calling start();
135 0 $self->{cache_passthrough} = 1;
136  
137 # Blocking
138 0 ▶ unless ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 0 0 0
139 0 my $tx_output = $self->start($tx_input);
140 0 $self->_cache_set_tx($tx_input, $tx_output);
141 0 return $tx_output;
142 }
143  
144 # Non-blocking
145 0 ▶ $self->start($tx_input, sub { $_[0]->_cache_set_tx($tx_input, $_[1])->$cb($_[1]) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
146 0 return $self;
147 }
148  
149 5 1;
150  
151 =encoding utf8
152  
153 =head1 NAME
154  
155 Mojo::UserAgent::Role::Cache - Role for Mojo::UserAgent that provides caching
156  
157 =head1 SYNOPSIS
158  
159 =head2 General
160  
161 # Apply the role
162 my $ua_class_with_cache = Mojo::UserAgent->with_roles('+Cache');
163 my $ua = $ua_class_with_cache->new;
164  
165 # Change the global cache driver
166 use CHI;
167 $ua_class_with_cache->cache_driver_singleton(CHI->new(driver => "Memory", datastore => {}));
168  
169 # Or change the driver for the instance
170 $ua->cache_driver(CHI->new(driver => "Memory", datastore => {}));
171  
172 # The rest is like a normal Mojo::UserAgent
173 my $tx = $ua->get($url)->error;
174  
175 =head2 Module
176  
177 package MyCoolModule;
178 use Mojo::Base -base;
179  
180 has ua => sub {
181 return $ENV{MOJO_USERAGENT_CACHE_STRATEGY}
182 ? Mojo::UserAgent->with_roles('+Cache') : Mojo::UserAgent->new;
183 };
184  
185 sub get_mojolicious_org {
186 return shift->ua->get("https://mojolicious.org/");
187 }
188  
189 Using the C<MOJO_USERAGENT_CACHE_STRATEGY> inside the module is a very
190 effective way to either use the global cache set up by a unit test, or run with
191 the default L<Mojo::UserAgent> without caching.
192  
193 =head2 Test
194  
195 use Mojo::Base -strict;
196 use Mojo::UserAgent::Role::Cache;
197 use MyCoolModule;
198 use Test::More;
199  
200 # Set up the environment and change the global cache_driver before running
201 # the tests
202 $ENV{MOJO_USERAGENT_CACHE_STRATEGY} ||= "playback";
203 Mojo::UserAgent::Role::Cache->cache_driver_singleton->root_dir("/some/path");
204  
205 # Run the tests
206 my $cool = MyCoolModule->new;
207 is $cool->get_mojolicious_org->res->code, 200, "mojolicious.org works";
208  
209 done_testing;
210  
211 =head1 DESCRIPTION
212  
213 L<Mojo::UserAgent::Role::Cache> is a role for the full featured non-blocking
214 I/O HTTP and WebSocket user agent L<Mojo::UserAgent>, that provides caching.
215  
216 The L</SYNOPSIS> shows how to use this in with tests, but there's nothing wrong
217 with using it for other things as well, where you want caching.
218  
219 By default, this module caches everything without any expiration. This is
220 because L<Mojo::UserAgent::Role::Cache::Driver::File> is very basic and
221 actually just meant for unit testing. If you want something more complex, you
222 can use L<CHI> or another L</cache_driver> that implements the logic you want.
223  
224 One exotic hack that is possible, is to make L</cache_key> return the whole
225 L<$tx> object and then implement a wrapper around L<CHI> that will investigate
226 the transaction and see if it wants to cache the request at all.
227  
228 =head1 WARNING
229  
230 =head2 Experimenntal
231  
232 L<Mojo::UserAgent::Role::Cache> is still under development, so there will be
233 changes and there is probably bugs that needs fixing. Please report in if you
234 find a bug or find this role interesting.
235  
236 L<https://github.com/jhthorsen/mojo-useragent-role-cache/issues>
237  
238 =head2 Upgrading from 0.02 to 0.03
239  
240 Upgrading from version 0.02 to 0.03 will cause all your cached files to be
241 invalid, since the L</cache_key> is changed. If you are using
242 L<Mojo::UserAgent::Role::Cache::Driver::File>, you can set the environment
243 variable C<MOJO_UA_CACHE_RENAME=1> to on-the-fly rename the old files to the
244 new format.
245  
246 =head1 ATTRIBUTES
247  
248 =head2 cache_driver
249  
250 $obj = $self->cache_driver;
251 $self = $self->cache_driver(CHI->new);
252  
253 Holds an object that will get/set the HTTP messages. Default is
254 L<Mojo::UserAgent::Role::Cache::Driver::File>, but any backend that supports
255 C<get()> and C<set()> should do. This means that you can use L<CHI> if you
256 like.
257  
258 =head2 cache_key
259  
260 $code = $self->cache_key;
261 $self = $self->cache_key(sub { my $tx = shift; return $tx->req->url });
262  
263 Holds a code ref that returns an array-ref of the key parts that is passed on
264 to C<get()> or C<set()> in the L</cache_driver>.
265  
266 This works with L<CHI> as well, since CHI will serialize the key if it is a
267 reference.
268  
269 The default is EXPERIMENTAL, but returns this value for now:
270  
271 [
272 $http_method, # get, post, ...
273 $host, # no port
274 $path_query, # /foo?x=42
275 md5($body), # but not for GET
276 ]
277  
278 =head2 cache_strategy
279  
280 $code = $self->cache_strategy;
281 $self = $self->cache_strategy(sub { my $tx = shift; return "passthrough" });
282  
283 Used to set up a callback to return a cache strategy. Default value is read
284 from the C<MOJO_USERAGENT_CACHE_STRATEGY> environment variable or
285 "playback_or_record".
286  
287 The return value from the C<$code> can be one of:
288  
289 =over 2
290  
291 =item * passthrough
292  
293 Will disable any caching.
294  
295 =item * playback
296  
297 Will never send a request to the remote server, but only look for recorded
298 messages.
299  
300 =item * playback_or_record
301  
302 Will return a recorded message if it exists, or fetch one from the remote
303 server and store the response.
304  
305 =item * record
306  
307 Will always fetch a new response from the remote server and store the response.
308  
309 =back
310  
311 =head1 METHODS
312  
313 =head2 cache_driver_singleton
314  
315 $obj = Mojo::UserAgent::Role::Cache->cache_driver_singleton;
316 Mojo::UserAgent::Role::Cache->cache_driver_singleton($obj);
317  
318 Used to retrieve or set the default L</cache_driver>. Useful for setting up
319 caching globally in unit tests.
320  
321 =head1 AUTHOR
322  
323 Jan Henning Thorsen
324  
325 =head1 COPYRIGHT AND LICENSE
326  
327 This program is free software, you can redistribute it and/or modify it under
328 the terms of the Artistic License version 2.0.
329  
330 =head1 SEE ALSO
331  
332 L<Mojo::UserAgent>,
333 L<https://metacpan.org/pod/Mojo::UserAgent::Cached> and
334 L<https://metacpan.org/pod/Mojo::UserAgent::Mockable>.
335  
336 =cut
↑ Summary Cache/Driver/File.pm »