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
|
|
90129
|
use strict; |
|
3
|
|
|
|
|
58
|
|
|
3
|
|
|
|
|
102
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
18
|
use base qw(LWP::UserAgent); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
4693
|
|
7
|
3
|
|
|
3
|
|
207084
|
use Cache::FileCache; |
|
3
|
|
|
|
|
158074
|
|
|
3
|
|
|
|
|
135
|
|
8
|
3
|
|
|
3
|
|
13081
|
use File::HomeDir; |
|
3
|
|
|
|
|
24483
|
|
|
3
|
|
|
|
|
233
|
|
9
|
3
|
|
|
3
|
|
27
|
use File::Spec; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
1822
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.12'; |
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
|
10985
|
my $class = shift; |
20
|
4
|
|
|
|
|
8
|
my $cache_opt; |
21
|
|
|
|
|
|
|
my %lwp_opt; |
22
|
4
|
100
|
|
|
|
19
|
unless (scalar @_ % 2) { |
23
|
2
|
|
|
|
|
7
|
%lwp_opt = @_; |
24
|
2
|
|
|
|
|
6
|
$cache_opt = {}; |
25
|
2
|
|
|
|
|
4
|
for my $key (qw(namespace cache_root default_expires_in)) { |
26
|
6
|
100
|
|
|
|
18
|
$cache_opt->{$key} = delete $lwp_opt{$key} if exists $lwp_opt{$key}; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
} else { |
29
|
2
|
|
50
|
|
|
11
|
$cache_opt = shift || {}; |
30
|
2
|
|
|
|
|
6
|
%lwp_opt = @_; |
31
|
|
|
|
|
|
|
} |
32
|
4
|
|
|
|
|
45
|
my $self = $class->SUPER::new(%lwp_opt); |
33
|
4
|
|
|
|
|
308907
|
my %cache_args = (%default_cache_args, %$cache_opt); |
34
|
4
|
|
|
|
|
56
|
$self->{cache} = Cache::FileCache->new(\%cache_args); |
35
|
4
|
|
|
|
|
737
|
return $self |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub request { |
39
|
3
|
|
|
3
|
1
|
24821
|
my $self = shift; |
40
|
3
|
|
|
|
|
8
|
my @args = @_; |
41
|
3
|
|
|
|
|
7
|
my $request = $args[0]; |
42
|
|
|
|
|
|
|
|
43
|
3
|
50
|
|
|
|
12
|
return $self->SUPER::request(@args) if $request->method ne 'GET'; |
44
|
|
|
|
|
|
|
|
45
|
3
|
|
|
|
|
48
|
my $uri = $request->uri->as_string; |
46
|
3
|
|
|
|
|
224
|
my $cache = $self->{cache}; |
47
|
3
|
|
|
|
|
28
|
my $obj = $cache->get( $uri ); |
48
|
|
|
|
|
|
|
|
49
|
3
|
50
|
|
|
|
2204
|
if ( defined $obj ) { |
50
|
|
|
|
|
|
|
|
51
|
3
|
100
|
66
|
|
|
19
|
if (defined $obj->{expires} and $obj->{expires} > time()) { |
52
|
1
|
|
|
|
|
9
|
return HTTP::Response->parse($obj->{as_string}); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
2
|
50
|
|
|
|
10
|
if (defined $obj->{last_modified}) { |
56
|
2
|
|
|
|
|
12
|
$request->header('If-Modified-Since' => |
57
|
|
|
|
|
|
|
HTTP::Date::time2str($obj->{last_modified})); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
2
|
100
|
|
|
|
266
|
if (defined $obj->{etag}) { |
61
|
1
|
|
|
|
|
5
|
$request->header('If-None-Match' => $obj->{etag}); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
2
|
|
|
|
|
45
|
$args[0] = $request; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
2
|
|
|
|
|
21
|
my $res = $self->SUPER::request(@args); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
## return cached data if it is "Not Modified" |
70
|
2
|
100
|
|
|
|
359313
|
if ($res->code eq HTTP::Status::RC_NOT_MODIFIED) { |
71
|
1
|
|
|
|
|
20
|
return HTTP::Response->parse($obj->{as_string}); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
## cache only "200 OK" content |
75
|
1
|
50
|
|
|
|
21
|
if ($res->code eq HTTP::Status::RC_OK) { |
76
|
0
|
|
|
|
|
0
|
$self->set_cache($uri, $res); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
1
|
|
|
|
|
23
|
return $res; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub set_cache { |
83
|
3
|
|
|
3
|
0
|
4566
|
my $self = shift; |
84
|
3
|
|
|
|
|
5
|
my ($uri, $res) = @_; |
85
|
3
|
|
|
|
|
7
|
my $cache = $self->{cache}; |
86
|
|
|
|
|
|
|
|
87
|
3
|
100
|
|
|
|
19
|
$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__ |