blib/lib/LWP/UserAgent/Cache/Memcached.pm

filter: Statementstmt 62.9% 39 / 62 Branchbran 15.0% 3 / 20 Conditioncond 14.2% 1 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 75.0% 6 / 8 Podpod 50.0% 2 / 4 totaltotal 48.5% 51 / 105 CC 14 SCAR 37.2
CC
14
Cov
48%
request
90.0
set_cache
12.0
cacher
3.0
CRAP 41.1
↑ Summary
1 package LWP::UserAgent::Cache::Memcached;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base qw(LWP::UserAgent);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.02';
9  
10 1 our $FAST = 1;
11 1 our %default_cache_args = (
12 'servers' => [ "127.0.0.1:11211" ],
13 'namespace' => 'lwp-cache',
14 'exptime' => 0,
15 );
16  
17 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
18 1 my $class = shift;
19 1 ▶ my $cache_opt = shift || {};
Condition50%1/2
shift() || {}
l!l
01
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
20 1 my $self = $class->SUPER::new(@_);
21 1 my %cache_args = (%default_cache_args, %$cache_opt);
22 $self->{lwp_useragent_cache_memcached_config} = {
23 exptime => $cache_args{exptime},
24 1 };
25 1 delete $cache_args{exptime};
26 1 $self->{cache} = $self->cacher->new(\%cache_args);
27 1 return $self
28 }
29  
30 5 ▶sub cacher {
Subroutine cacher: called CC=3 SCAR=11.0
Pod undocumented
31 5 my @cacher = qw/Cache::Memcached::Fast Cache::Memcached/;
32 5 my $cacher;
33  
34 5 ▶ if ($FAST) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LWP::UserAgent::Cache::Memcached::FAST) { } 1 4 5
35 1 $cacher = $cacher[0];
36 1 eval {load $cacher};
37 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
38 1 $cacher = $cacher[1];
39 1 eval {load $cacher};
40 1 $FAST = 0;
41 }
42 }
43 else {
44 4 $cacher = $cacher[1];
45 4 eval {load $cacher};
46 }
47  
48 5 return $cacher;
49 }
50  
51 0 ▶sub request {
Subroutine request: not called CC=9 SCAR=45.0
Pod documented
52 0 my $self = shift;
53 0 my @args = @_;
54 0 my $request = $args[0];
55  
56 0 ▶ return $self->SUPER::request(@args) if $request->method ne 'GET';
Branch: 1 branch
BranchTrueFalseTotal
if $request->method ne "GET" 0 0 0
57  
58 0 my $uri = $request->uri->as_string;
59 0 my $cache = $self->{cache};
60 0 my $obj = $cache->get( $uri );
61  
62 0 ▶ if ( defined $obj ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj) 0 0 0
63  
64 0 ▶ unless (defined $obj->{expires} and $obj->{expires} <= time()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $obj->{"expires"} and $obj->{"expires"} <= time) 0 0 0
Condition0%0/3
defined $obj->{"expires"} and $obj->{"expires"} <= time
!ll&&!rl&&r
000
MC/DC0%0/2
defined $obj->{"expires"} and $obj->{"expires"} <= time
defined $obj->{"expires"} $obj->{"expires"} <= time
Truth table0%0/3
A and B
A= defined $obj->{"expires"}B= $obj->{"expires"} <= time
ABresult
0X0
100
111
65 0 return HTTP::Response->parse($obj->{as_string});
66 }
67  
68 0 ▶ if (defined $obj->{last_modified}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj->{"last_modified"}) 0 0 0
69 $request->header(
70 'If-Modified-Since' => HTTP::Date::time2str($obj->{last_modified})
71 0 );
72 }
73  
74 0 ▶ if (defined $obj->{etag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj->{"etag"}) 0 0 0
75 0 $request->header('If-None-Match' => $obj->{etag});
76 }
77  
78 0 $args[0] = $request;
79 }
80  
81 0 my $res = $self->SUPER::request(@args);
82 0 ▶ my $exptime = int($self->{lwp_useragent_cache_memcached_config}->{exptime} || 0);
Condition0%0/2
$self->{'lwp_useragent_cache_memcached_config'}{'exptime'} || 0
l!l
00
MC/DC0%0/1
$self->{'lwp_useragent_cache_memcached_config'}{'exptime'} || 0
$self->{'lwp_useragent_cache_memcached_config'}{'exptime'}
Truth table0%0/2
A
A= $self->{'lwp_useragent_cache_memcached_config'}{'exptime'}
Aresult
11
00
83 0 ▶ $self->set_cache($uri, $res, $exptime) if $res->code eq HTTP::Status::RC_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $res->code eq 200 0 0 0
84  
85 0 return $res;
86 }
87  
88 0 ▶sub set_cache {
Subroutine set_cache: not called CC=3 SCAR=24.8
Pod undocumented
89 0 my $self = shift;
90 0 my ($uri, $res, $exptime) = @_;
91 0 my $cache = $self->{cache};
92  
93 0 ▶ $cache->set($uri,{
Branch: 2 branches
BranchTrueFalseTotal
$res->header('Etag') ? : 0 0 0
$res->expires ? : 0 0 0
94 content => $res->content,
95 last_modified => $res->last_modified,
96 etag => $res->header('Etag') ? $res->header('Etag') : undef,
97 expires => $res->expires ? $res->expires : undef,
98 as_string => $res->as_string,
99 },$exptime);
100 }
101  
102 1 1;
103 __END__
↑ Summary