| 1 | # $Id: WithCache.pm,v 1.4 2005/02/23 11:25:44 sekimura Exp $ | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package LWP::UserAgent::WithCache; | ||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use base qw(LWP::UserAgent); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Cache::FileCache; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use File::HomeDir; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | our $VERSION = '0.13'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | 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 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 4 | my $class = shift; | |||||||||||||||||||||||||||
| 20 | 4 | my $cache_opt; | |||||||||||||||||||||||||||
| 21 | my %lwp_opt; | ||||||||||||||||||||||||||||
| 22 | 4 | ▶ | unless (scalar @_ % 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 2 | %lwp_opt = @_; | |||||||||||||||||||||||||||
| 24 | 2 | $cache_opt = {}; | |||||||||||||||||||||||||||
| 25 | 2 | for my $key (qw(namespace cache_root default_expires_in)) { | |||||||||||||||||||||||||||
| 26 | 6 | ▶ | $cache_opt->{$key} = delete $lwp_opt{$key} if exists $lwp_opt{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | } else { | ||||||||||||||||||||||||||||
| 29 | 2 | ▶ | $cache_opt = shift || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 30 | 2 | %lwp_opt = @_; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 4 | my $self = $class->SUPER::new(%lwp_opt); | |||||||||||||||||||||||||||
| 33 | 4 | my %cache_args = (%default_cache_args, %$cache_opt); | |||||||||||||||||||||||||||
| 34 | 4 | $self->{cache} = Cache::FileCache->new(\%cache_args); | |||||||||||||||||||||||||||
| 35 | 4 | return $self | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 4 | ▶ | sub request { | ||||||||||||||||||||||||||
|
Subroutine
request: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 40 | 4 | my @args = @_; | |||||||||||||||||||||||||||
| 41 | 4 | my $request = $args[0]; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 4 | ▶ | return $self->SUPER::request(@args) if $request->method ne 'GET'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 4 | my $uri = $request->uri->as_string; | |||||||||||||||||||||||||||
| 46 | 4 | my $cache = $self->{cache}; | |||||||||||||||||||||||||||
| 47 | 4 | my $obj = $cache->get( $uri ); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 4 | ▶ | if ( defined $obj ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 3 | ▶ | if (defined $obj->{expires} and $obj->{expires} > time()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $obj->{"expires"} and $obj->{"expires"} > time
MC/DC50%1/2
defined $obj->{"expires"} and $obj->{"expires"} > time
defined $obj->{"expires"}
$obj->{"expires"} > time
Truth table66%2/3
A and B
A= defined $obj->{"expires"}B= $obj->{"expires"} > time
| |||||||||||||||||||||||||||||
| 52 | 1 | return HTTP::Response->parse($obj->{as_string}); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 2 | ▶ | if (defined $obj->{last_modified}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | $request->header('If-Modified-Since' => | ||||||||||||||||||||||||||||
| 57 | 2 | HTTP::Date::time2str($obj->{last_modified})); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 2 | ▶ | if (defined $obj->{etag}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 1 | $request->header('If-None-Match' => $obj->{etag}); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | $args[0] = $request; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 3 | my $res = $self->SUPER::request(@args); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | ## return cached data if it is "Not Modified" | ||||||||||||||||||||||||||||
| 70 | 3 | ▶ | if ($res->code eq HTTP::Status::RC_NOT_MODIFIED) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 1 | return HTTP::Response->parse($obj->{as_string}); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | ## cache only "200 OK" content | ||||||||||||||||||||||||||||
| 75 | 2 | ▶ | if ($res->code eq HTTP::Status::RC_OK) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 2 | $self->set_cache($uri, $res); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 2 | return $res; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 5 | ▶ | sub set_cache { | ||||||||||||||||||||||||||
|
Subroutine
set_cache: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 84 | 5 | my ($uri, $res) = @_; | |||||||||||||||||||||||||||
| 85 | 5 | my $cache = $self->{cache}; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 5 | ▶ | $cache->set($uri,{ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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 | 3 | 1; | |||||||||||||||||||||||||||
| 97 | __END__ | ||||||||||||||||||||||||||||