File Coverage

blib/lib/LWP/UserAgent/Caching.pm
Criterion Covered Total %
statement 17 39 43.5
branch 0 4 0.0
condition 2 4 50.0
subroutine 5 12 41.6
pod 7 7 100.0
total 31 66 46.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.04';
10              
11 1     1   1754 use strict;
  1         2  
  1         28  
12 1     1   3 use warnings;
  1         2  
  1         22  
13              
14 1     1   426 use parent 'LWP::UserAgent';
  1         226  
  1         3  
15 1     1   29744 use HTTP::Caching;
  1         46067  
  1         347  
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 inherrits all those. In
64             this module we also implemented the shortcuts from L so
65             that tehy will not call the parrent 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 3815 my ( $class, %params ) = @_;
76              
77 2   50     8 my $http_caching = delete $params{http_caching} || {};
78              
79 2         11 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     2146 );
88              
89 2         2040 return $self;
90             }
91              
92             sub request {
93 0     0 1   return shift->{http_caching}->make_request(@_);
94             }
95              
96              
97             #
98             # Now the shortcuts...
99             #
100             sub get {
101 0     0 1   require HTTP::Request::Common;
102 0           my($self, @parameters) = @_;
103 0           my @suff = $self->_process_colonic_headers(\@parameters,1);
104 0           return $self->request( HTTP::Request::Common::GET( @parameters ), @suff );
105             }
106              
107             sub post {
108 0     0 1   require HTTP::Request::Common;
109 0           my($self, @parameters) = @_;
110 0 0         my @suff = $self->_process_colonic_headers(\@parameters, (ref($parameters[1]) ? 2 : 1));
111 0           return $self->request( HTTP::Request::Common::POST( @parameters ), @suff );
112             }
113              
114             sub head {
115 0     0 1   require HTTP::Request::Common;
116 0           my($self, @parameters) = @_;
117 0           my @suff = $self->_process_colonic_headers(\@parameters,1);
118 0           return $self->request( HTTP::Request::Common::HEAD( @parameters ), @suff );
119             }
120              
121             sub put {
122 0     0 1   require HTTP::Request::Common;
123 0           my($self, @parameters) = @_;
124 0 0         my @suff = $self->_process_colonic_headers(\@parameters, (ref($parameters[1]) ? 2 : 1));
125 0           return $self->request( HTTP::Request::Common::PUT( @parameters ), @suff );
126             }
127              
128             sub delete {
129 0     0 1   require HTTP::Request::Common;
130 0           my($self, @parameters) = @_;
131 0           my @suff = $self->_process_colonic_headers(\@parameters,1);
132 0           return $self->request( HTTP::Request::Common::DELETE( @parameters ), @suff );
133             }
134              
135              
136             1;