Role/CHICaching/SimpleKeyGen.pm

filter: Statementstmt 100% 18 / 18 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 100% 27 / 27 CC 2 SCAR 2.5
CC
2
Cov
100%
cache_vary
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.3
« Role/CHICaching/VaryNotAsterisk.pm ↑ Summary CHICaching.pm »
1 package LWP::UserAgent::Role::CHICaching::SimpleKeyGen;
2  
3 3 ▶use 5.006000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use CHI;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Types::Standard qw(Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $AUTHORITY = 'cpan:KJETILK';
9 3 our $VERSION = '0.04';
10  
11 =pod
12  
13 =encoding utf-8
14  
15 =head1 NAME
16  
17 LWP::UserAgent::Role::CHICaching::SimpleKeyGen - A role for cache keys when caching LWP::UserAgent
18  
19 =head1 SYNOPSIS
20  
21 See L<LWP::UserAgent::Role::CHICaching>.
22  
23  
24 =head1 DESCRIPTION
25  
26 L<LWP::UserAgent::Role::CHICaching> is a role for creating caching
27 user agents. There's some complexity around caching different variants
28 of the same resource (e.g. the same thing in different natural
29 languages, different serializations that is considered in L<Section
30 4.1 of RFC7234|http://tools.ietf.org/html/rfc7234#section-4.1> that
31 this role is factored out to address in the dumbest way possible: Just
32 don't cache when the problem arises.
33  
34 To really solve this problem in a better way, you need to generate a
35 cache key based on not only the URI, but also on the content
36 (e.g. C<Content-Language: en>), and so, provide a better
37 implementation of the C<key> attribute, and then, you also need to
38 tell the system when it is OK to cache something with a C<Vary> header
39 by making the C<cache_vary> method smarter. See
40 L<LWP::UserAgent::Role::CHICaching::VaryNotAsterisk> for an example of
41 an alternative.
42  
43 =head2 Attributes and Methods
44  
45 =over
46  
47 =item C<< key >>, C<< clear_key >>
48  
49 The key to use for a response. This role will return the canonical URI of the
50 request as a string, which is a reasonable default.
51  
52 =item C<< cache_vary >>
53  
54 Will never allow a response with a C<Vary> header to be cached.
55  
56 =back
57  
58 =cut
59  
60  
61 3 has key => (
62 is => 'rw',
63 isa => Str,
64 lazy => 1,
65 clearer => 1,
66 builder => '_build_key'
67 );
68  
69 6 ▶sub _build_key { return shift->request_uri->canonical->as_string }
Subroutine _build_key: called CC=1 SCAR=0.0
70  
71 10 ▶sub cache_vary {
Subroutine cache_vary: called CC=2 SCAR=6.9
Pod documented
72 10 my ($self, $res) = @_;
73 10 ▶ return (defined($res->header('Vary')) ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $res->header('Vary') ? : 4 6 10
74 }
75  
76 3 1;
77  
78 __END__
« Role/CHICaching/VaryNotAsterisk.pm ↑ Summary CHICaching.pm »