File Coverage

blib/lib/LWP/UserAgent/WithCache.pm
Criterion Covered Total %
statement 53 53 100.0
branch 19 22 86.3
condition 3 5 60.0
subroutine 8 8 100.0
pod 2 3 66.6
total 85 91 93.4


line stmt bran cond sub pod time code
1             # $Id: WithCache.pm,v 1.4 2005/02/23 11:25:44 sekimura Exp $
2              
3             package LWP::UserAgent::WithCache;
4 3     3   189153 use strict;
  3         20  
  3         83  
5              
6 3     3   14 use base qw(LWP::UserAgent);
  3         4  
  3         1954  
7 3     3   123627 use Cache::FileCache;
  3         86700  
  3         119  
8 3     3   1216 use File::HomeDir;
  3         13608  
  3         142  
9 3     3   36 use File::Spec;
  3         6  
  3         1324  
10              
11             our $VERSION = '0.13';
12              
13             our %default_cache_args = (
14             'namespace' => 'lwp-cache',
15             'cache_root' => File::Spec->catfile(File::HomeDir->my_home, '.cache'),
16             'default_expires_in' => 600 );
17              
18             sub new {
19 4     4 1 2852 my $class = shift;
20 4         7 my $cache_opt;
21             my %lwp_opt;
22 4 100       15 unless (scalar @_ % 2) {
23 2         8 %lwp_opt = @_;
24 2         4 $cache_opt = {};
25 2         5 for my $key (qw(namespace cache_root default_expires_in)) {
26 6 100       17 $cache_opt->{$key} = delete $lwp_opt{$key} if exists $lwp_opt{$key};
27             }
28             } else {
29 2   50     8 $cache_opt = shift || {};
30 2         6 %lwp_opt = @_;
31             }
32 4         28 my $self = $class->SUPER::new(%lwp_opt);
33 4         5567 my %cache_args = (%default_cache_args, %$cache_opt);
34 4         27 $self->{cache} = Cache::FileCache->new(\%cache_args);
35 4         869 return $self
36             }
37              
38             sub request {
39 4     4 1 155173 my $self = shift;
40 4         26 my @args = @_;
41 4         8 my $request = $args[0];
42              
43 4 50       20 return $self->SUPER::request(@args) if $request->method ne 'GET';
44              
45 4         49 my $uri = $request->uri->as_string;
46 4         96 my $cache = $self->{cache};
47 4         25 my $obj = $cache->get( $uri );
48              
49 4 100       1930 if ( defined $obj ) {
50              
51 3 100 66     15 if (defined $obj->{expires} and $obj->{expires} > time()) {
52 1         8 return HTTP::Response->parse($obj->{as_string});
53             }
54              
55 2 50       12 if (defined $obj->{last_modified}) {
56             $request->header('If-Modified-Since' =>
57 2         10 HTTP::Date::time2str($obj->{last_modified}));
58             }
59              
60 2 100       148 if (defined $obj->{etag}) {
61 1         5 $request->header('If-None-Match' => $obj->{etag});
62             }
63              
64 2         47 $args[0] = $request;
65             }
66              
67 3         38 my $res = $self->SUPER::request(@args);
68              
69             ## return cached data if it is "Not Modified"
70 3 100       163976 if ($res->code eq HTTP::Status::RC_NOT_MODIFIED) {
71 1         16 return HTTP::Response->parse($obj->{as_string});
72             }
73              
74             ## cache only "200 OK" content
75 2 50       41 if ($res->code eq HTTP::Status::RC_OK) {
76 2         37 $self->set_cache($uri, $res);
77             }
78              
79 2         4898 return $res;
80             }
81              
82             sub set_cache {
83 5     5 0 3008 my $self = shift;
84 5         15 my ($uri, $res) = @_;
85 5         11 my $cache = $self->{cache};
86              
87 5 100       22 $cache->set($uri,{
    100          
88             content => $res->content,
89             last_modified => $res->last_modified,
90             etag => $res->header('Etag') ? $res->header('Etag') : undef,
91             expires => $res->expires ? $res->expires : undef,
92             as_string => $res->as_string,
93             });
94             }
95              
96             1;
97             __END__