| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package LWP::UserAgent::Cache::Memcached; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 29433 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 45 |  | 
| 4 | 1 |  |  | 1 |  | 6 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 33 |  | 
| 5 | 1 |  |  | 1 |  | 6 | use base qw(LWP::UserAgent); | 
|  | 1 |  |  |  |  | 6 |  | 
|  | 1 |  |  |  |  | 1245 |  | 
| 6 | 1 |  |  | 1 |  | 60370 | use Module::Load; | 
|  | 1 |  |  |  |  | 1394 |  | 
|  | 1 |  |  |  |  | 7 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | our $VERSION = '0.02'; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | our $FAST = 1; | 
| 11 |  |  |  |  |  |  | our %default_cache_args = ( | 
| 12 |  |  |  |  |  |  | 'servers' => [ "127.0.0.1:11211" ], | 
| 13 |  |  |  |  |  |  | 'namespace' => 'lwp-cache', | 
| 14 |  |  |  |  |  |  | 'exptime' => 0, | 
| 15 |  |  |  |  |  |  | ); | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | sub new { | 
| 18 | 1 |  |  | 1 | 1 | 13 | my $class = shift; | 
| 19 | 1 |  | 50 |  |  | 7 | my $cache_opt = shift || {}; | 
| 20 | 1 |  |  |  |  | 12 | my $self = $class->SUPER::new(@_); | 
| 21 | 1 |  |  |  |  | 3600 | my %cache_args = (%default_cache_args, %$cache_opt); | 
| 22 | 1 |  |  |  |  | 5 | $self->{lwp_useragent_cache_memcached_config} = { | 
| 23 |  |  |  |  |  |  | exptime => $cache_args{exptime}, | 
| 24 |  |  |  |  |  |  | }; | 
| 25 | 1 |  |  |  |  | 3 | delete $cache_args{exptime}; | 
| 26 | 1 |  |  |  |  | 5 | $self->{cache} = $self->cacher->new(\%cache_args); | 
| 27 | 1 |  |  |  |  | 4192 | return $self | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | sub cacher { | 
| 31 | 5 |  |  | 5 | 0 | 22 | my @cacher = qw/Cache::Memcached::Fast Cache::Memcached/; | 
| 32 | 5 |  |  |  |  | 7 | my $cacher; | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 5 | 100 |  |  |  | 14 | if ($FAST) { | 
| 35 | 1 |  |  |  |  | 2 | $cacher = $cacher[0]; | 
| 36 | 1 |  |  |  |  | 3 | eval {load $cacher}; | 
|  | 1 |  |  |  |  | 4 |  | 
| 37 | 1 | 50 |  |  |  | 703 | if ($@) { | 
| 38 | 1 |  |  |  |  | 3 | $cacher = $cacher[1]; | 
| 39 | 1 |  |  |  |  | 2 | eval {load $cacher}; | 
|  | 1 |  |  |  |  | 4 |  | 
| 40 | 1 |  |  |  |  | 44 | $FAST = 0; | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  | else { | 
| 44 | 4 |  |  |  |  | 6 | $cacher = $cacher[1]; | 
| 45 | 4 |  |  |  |  | 5 | eval {load $cacher}; | 
|  | 4 |  |  |  |  | 15 |  | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 5 |  |  |  |  | 248234 | return $cacher; | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub request { | 
| 52 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 53 | 0 |  |  |  |  |  | my @args = @_; | 
| 54 | 0 |  |  |  |  |  | my $request = $args[0]; | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 0 | 0 |  |  |  |  | return $self->SUPER::request(@args) if $request->method ne 'GET'; | 
| 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 | 0 |  |  |  |  | if ( defined $obj ) { | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 0 | 0 | 0 |  |  |  | unless (defined $obj->{expires} and $obj->{expires} <= time()) { | 
| 65 | 0 |  |  |  |  |  | return HTTP::Response->parse($obj->{as_string}); | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 0 | 0 |  |  |  |  | if (defined $obj->{last_modified}) { | 
| 69 | 0 |  |  |  |  |  | $request->header( | 
| 70 |  |  |  |  |  |  | 'If-Modified-Since' => HTTP::Date::time2str($obj->{last_modified}) | 
| 71 |  |  |  |  |  |  | ); | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 0 | 0 |  |  |  |  | if (defined $obj->{etag}) { | 
| 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 |  | 0 |  |  |  | my $exptime = int($self->{lwp_useragent_cache_memcached_config}->{exptime} || 0); | 
| 83 | 0 | 0 |  |  |  |  | $self->set_cache($uri, $res, $exptime) if $res->code eq HTTP::Status::RC_OK; | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 0 |  |  |  |  |  | return $res; | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | sub set_cache { | 
| 89 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 90 | 0 |  |  |  |  |  | my ($uri, $res, $exptime) = @_; | 
| 91 | 0 |  |  |  |  |  | my $cache = $self->{cache}; | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 0 | 0 |  |  |  |  | $cache->set($uri,{ | 
|  |  | 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; | 
| 103 |  |  |  |  |  |  | __END__ |