File Coverage

blib/lib/LWP/UserAgent/Caching.pm
Criterion Covered Total %
statement 17 42 40.4
branch 0 4 0.0
condition 2 4 50.0
subroutine 5 12 41.6
pod 7 7 100.0
total 31 69 44.9


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.07';
10              
11 1     1   2194 use strict;
  1         1  
  1         23  
12 1     1   4 use warnings;
  1         1  
  1         23  
13              
14 1     1   370 use parent 'LWP::UserAgent';
  1         242  
  1         4  
15 1     1   37637 use HTTP::Caching;
  1         57173  
  1         444  
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            
32             my $ua = LWP::UserAgent::Caching->new(
33             http_caching => {
34             cache => $cache,
35             type => 'private',
36             request_directives => (
37             'max-age=86400', # 24hrs
38             'min-fresh=60', # not over due within the next minute
39             ),
40             },
41             # more LWP::UserAgent options
42             );
43            
44             my $rqst = HTTP::Request->new( GET => 'http://example.com' );
45            
46             $rqst->header( cache_control => 'no-cache' ); # Oh... now we bypass it ?
47             $rqst->header( accept_language => 'nl, en-GB; q=0.9, en; 0.8, *' );
48            
49             my $resp = $ua->request($rqst);
50              
51              
52             =head1 DESCRIPTION
53              
54             C gives you RFC compliant caching. It respects the old
55             HTTP/1 headerfields like 'Expires' but also implements the HTTP/1.1
56             'Cache-Control' directives.
57              
58             Unlike many other cachng useragents, this one does actually invalidate the cache
59             after a non-error response returned by a non-safe request (like DELETE).
60              
61             =head1 METHODS
62              
63             Since it's a subclass of the standard LWP::UserAgent, it inherits all those. In
64             this module we also implemented the shortcuts from L so
65             that they will not call the parent class
66              
67             =head1 SEE ALSO
68              
69             L The RFC 7234 compliant brains
70             - DO NEVER USE THAT MODULE DIRECTLY
71              
72             =cut
73              
74             sub new {
75 2     2 1 4762 my ( $class, %params ) = @_;
76              
77 2   50     9 my $http_caching = delete $params{http_caching} || {};
78              
79 2         10 my $self = $class->SUPER::new(@_);
80              
81             $self->{http_caching} = HTTP::Caching->new(
82             cache => $http_caching->{cache},
83             # cache_meta => $http_caching->{cache_meta} || $params{cache},
84             cache_type => $http_caching->{type} || 'private',
85             cache_control_request => $http_caching->{request_directives},
86 0     0   0 forwarder => sub { $self->SUPER::request(@_) }
87 2   50     2723 );
88              
89 2         2695 return $self;
90             }
91              
92             sub request {
93 0     0 1   my $self = shift;
94 0           my $rqst = shift->clone;
95 0           $self->prepare_request($rqst);
96 0           return $self->{http_caching}->make_request($rqst, @_);
97             }
98              
99              
100             #
101             # Now the shortcuts...
102             #
103             sub get {
104 0     0 1   require HTTP::Request::Common;
105 0           my($self, @parameters) = @_;
106 0           my @suff = $self->_process_colonic_headers(\@parameters,1);
107 0           return $self->request( HTTP::Request::Common::GET( @parameters ), @suff );
108             }
109              
110             sub post {
111 0     0 1   require HTTP::Request::Common;
112 0           my($self, @parameters) = @_;
113 0 0         my @suff = $self->_process_colonic_headers(\@parameters, (ref($parameters[1]) ? 2 : 1));
114 0           return $self->request( HTTP::Request::Common::POST( @parameters ), @suff );
115             }
116              
117             sub head {
118 0     0 1   require HTTP::Request::Common;
119 0           my($self, @parameters) = @_;
120 0           my @suff = $self->_process_colonic_headers(\@parameters,1);
121 0           return $self->request( HTTP::Request::Common::HEAD( @parameters ), @suff );
122             }
123              
124             sub put {
125 0     0 1   require HTTP::Request::Common;
126 0           my($self, @parameters) = @_;
127 0 0         my @suff = $self->_process_colonic_headers(\@parameters, (ref($parameters[1]) ? 2 : 1));
128 0           return $self->request( HTTP::Request::Common::PUT( @parameters ), @suff );
129             }
130              
131             sub delete {
132 0     0 1   require HTTP::Request::Common;
133 0           my($self, @parameters) = @_;
134 0           my @suff = $self->_process_colonic_headers(\@parameters,1);
135 0           return $self->request( HTTP::Request::Common::DELETE( @parameters ), @suff );
136             }
137              
138              
139             1;