File Coverage

blib/lib/LWP/UserAgent/Role/CHICaching/SimpleKeyGen.pm
Criterion Covered Total %
statement 14 14 100.0
branch 2 2 100.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 23 23 100.0


line stmt bran cond sub pod time code
1             package LWP::UserAgent::Role::CHICaching::SimpleKeyGen;
2              
3 3     3   1440 use 5.006000;
  3         8  
4 3     3   11 use CHI;
  3         3  
  3         43  
5 3     3   9 use Moo::Role;
  3         4  
  3         16  
6 3     3   628 use Types::Standard qw(Str);
  3         4  
  3         20  
7              
8             our $AUTHORITY = 'cpan:KJETILK';
9             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             has key => (
62             is => 'rw',
63             isa => Str,
64             lazy => 1,
65             clearer => 1,
66             builder => '_build_key'
67             );
68              
69 6     6   1285 sub _build_key { return shift->request_uri->canonical->as_string }
70              
71             sub cache_vary {
72 10     10 1 7486 my ($self, $res) = @_;
73 10 100       26 return (defined($res->header('Vary')) ? 0 : 1);
74             }
75              
76             1;
77              
78             __END__
79              
80              
81             =head1 AUTHOR
82              
83             Kjetil Kjernsmo E<lt>kjetilk@cpan.orgE<gt>.
84              
85             =head1 COPYRIGHT AND LICENCE
86              
87             This software is copyright (c) 2015, 2016 by Kjetil Kjernsmo.
88              
89             This is free software; you can redistribute it and/or modify it under
90             the same terms as the Perl 5 programming language system itself.