| 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() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 35 | 1 | $cacher = $cacher[0]; | |||||||||||||||||||||||||||
| 36 | 1 | eval {load $cacher}; | |||||||||||||||||||||||||||
| 37 | 1 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | unless (defined $obj->{expires} and $obj->{expires} <= time()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $obj->{"expires"} and $obj->{"expires"} <= time
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
| |||||||||||||||||||||||||||||
| 65 | 0 | return HTTP::Response->parse($obj->{as_string}); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if (defined $obj->{last_modified}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
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'}
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $self->set_cache($uri, $res, $exptime) if $res->code eq HTTP::Status::RC_OK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||