File Coverage

blib/lib/LWP/UserAgent/Caching.pm
Criterion Covered Total %
statement 16 38 42.1
branch 0 4 0.0
condition 1 2 50.0
subroutine 5 12 41.6
pod 7 7 100.0
total 29 63 46.0


line stmt bran cond sub pod time code
1             package LWP::UserAgent::Caching;
2              
3             =head1 NAME
4              
5             LWP::UserAgent::Caching - HTTP::Casing based UserAgent, finally done right
6              
7             =cut
8              
9             our $VERSION = '0.03';
10              
11 1     1   2120 use strict;
  1         1  
  1         22  
12 1     1   4 use warnings;
  1         1  
  1         23  
13              
14 1     1   369 use parent 'LWP::UserAgent';
  1         221  
  1         5  
15 1     1   29603 use HTTP::Caching;
  1         46394  
  1         338  
16              
17             =head1 SYNOPSIS
18              
19             use LWP::UserAgent::Caching;
20            
21             my $cache = CHI->new(
22             driver => 'File',
23             root_dir => '/tmp/LWP_UserAgent_Caching',
24             file_extension => '.cache',
25             l1_cache => {
26             driver => 'Memory',
27             global => 1,
28             max_size => 1024*1024
29             );
30            
31             my $ua = LWP::UserAgent::Caching->new(
32             cache => $cache,
33             cache_type => 'private',
34             cache_control => (
35             'max-age=86400', # 24hrs
36             'min-fresh=60', # not over due within the next minute
37             )
38             );
39            
40             my $rqst = HTTP::Request->new( GET => 'http://example.com' );
41            
42             $rqst->header( cache_control => 'no-cache' ); # Oh... now we bypass it ?
43             $rqst->header( accept_language => 'nl, en-GB; q=0.9, en; 0.8, *' );
44            
45             my $resp = $ua->request($rqst);
46              
47              
48             =head1 DESCRIPTION
49              
50             C gives you RFC compliant caching. It respects the old
51             HTTP/1 headerfields like 'Expires' but also implements the HTTP/1.1
52             'Cache-Control' directives.
53              
54             Unlike many other cachng useragents, this one does actually invalidate the cache
55             after a non-error response returned by a non-safe request (like DELETE).
56              
57             =head1 METHODS
58              
59             Since it's a subclass of the standard LWP::UserAgent, it inherrits all those. In
60             this module we also implemented the shortcuts from L so
61             that tehy will not call the parrent class
62              
63             =head1 SEE ALSO
64              
65             L The RFC 7234 compliant brains
66             - DO NEVER USE THAT MODULE DIRECTLY
67              
68             =cut
69              
70             sub new {
71 2     2 1 4800 my ( $class, %params ) = @_;
72              
73 2         12 my $self = $class->SUPER::new(@_);
74              
75             $self->{http_caching} = HTTP::Caching->new(
76             cache => $params{cache},
77             # cache_meta => $params{cache_meta} || $params{cache},
78             cache_type => $params{cache_type} || 'private',
79             cache_control_request => $params{cache_control},
80 0     0   0 forwarder => sub { $self->SUPER::request(@_) }
81 2   50     2229 );
82              
83 2         1928 return $self;
84             }
85              
86             sub request {
87 0     0 1   return shift->{http_caching}->make_request(@_);
88             }
89              
90              
91             #
92             # Now the shortcuts...
93             #
94             sub get {
95 0     0 1   require HTTP::Request::Common;
96 0           my($self, @parameters) = @_;
97 0           my @suff = $self->_process_colonic_headers(\@parameters,1);
98 0           return $self->request( HTTP::Request::Common::GET( @parameters ), @suff );
99             }
100              
101             sub post {
102 0     0 1   require HTTP::Request::Common;
103 0           my($self, @parameters) = @_;
104 0 0         my @suff = $self->_process_colonic_headers(\@parameters, (ref($parameters[1]) ? 2 : 1));
105 0           return $self->request( HTTP::Request::Common::POST( @parameters ), @suff );
106             }
107              
108             sub head {
109 0     0 1   require HTTP::Request::Common;
110 0           my($self, @parameters) = @_;
111 0           my @suff = $self->_process_colonic_headers(\@parameters,1);
112 0           return $self->request( HTTP::Request::Common::HEAD( @parameters ), @suff );
113             }
114              
115             sub put {
116 0     0 1   require HTTP::Request::Common;
117 0           my($self, @parameters) = @_;
118 0 0         my @suff = $self->_process_colonic_headers(\@parameters, (ref($parameters[1]) ? 2 : 1));
119 0           return $self->request( HTTP::Request::Common::PUT( @parameters ), @suff );
120             }
121              
122             sub delete {
123 0     0 1   require HTTP::Request::Common;
124 0           my($self, @parameters) = @_;
125 0           my @suff = $self->_process_colonic_headers(\@parameters,1);
126 0           return $self->request( HTTP::Request::Common::DELETE( @parameters ), @suff );
127             }
128              
129              
130             1;