File Coverage

blib/lib/AnyEvent/DNS/Cache/Simple.pm
Criterion Covered Total %
statement 57 63 90.4
branch 15 22 68.1
condition 1 2 50.0
subroutine 12 12 100.0
pod 2 2 100.0
total 87 101 86.1


line stmt bran cond sub pod time code
1             package AnyEvent::DNS::Cache::Simple;
2              
3 4     4   9502 use 5.008005;
  4         15  
  4         160  
4 4     4   23 use strict;
  4         7  
  4         121  
5 4     4   21 use warnings;
  4         16  
  4         125  
6 4     4   19 use base qw/AnyEvent::DNS/;
  4         6  
  4         4671  
7 4     4   195016 use Cache::Memory::Simple;
  4         12011  
  4         129  
8 4     4   31 use List::Util qw//;
  4         8  
  4         64  
9 4     4   21 use Time::HiRes qw//;
  4         8  
  4         1883  
10              
11             our $VERSION = "0.01";
12              
13             sub request($$) {
14 4     4 1 4204 my ($self, $req, $cb) = @_;
15 4         8 my ($name, $qtype, $class) = @{$req->{qd}[0]};
  4         16  
16 4         12 my $cache_key = "$class $qtype $name"; #compatibility with Net::DNS::Lite
17 4 100       21 if ( my $cached = $self->{adcs_cache}->get($cache_key) ) {
18 2         32 my ($res,$expires_at) = @$cached;
19 2 50       9 if ( $expires_at < Time::HiRes::time ) {
20 0         0 undef $res;
21 0         0 $self->{adcs_cache}->remove($cache_key)
22             }
23 2 50       6 if ( !defined $res ) {
24 0         0 $cb->();
25 0         0 return;
26             }
27 2         6 return $cb->($res);
28             }
29              
30             # request
31             $self->SUPER::request($req, sub {
32 2     2   2001803 my ($res) = @_;
33 2 100       17 if ( !@_ ) {
34 1         17 $self->{adcs_cache}->set($cache_key, [undef, $self->{adcs_negative_ttl} + Time::HiRes::time() ], $self->{adcs_negative_ttl});
35 1         16 return $cb->();
36             }
37 11         25 my $ttl = List::Util::min(
38             $self->{adcs_ttl},
39             map {
40 1         6 $_->[3]
41 1 50       3 } (@{$res->{an}} ? @{$res->{an}} : @{$res->{ns}}),
  1         3  
  0         0  
42             );
43 1         24 $self->{adcs_cache}->set($cache_key, [$res, $ttl + Time::HiRes::time ], $ttl);
44 1         12 $cb->($res);
45 2         55 });
46             }
47              
48             sub register {
49 2     2 1 8131 my $class = shift;
50 2 50       20 my %args = @_ == 1 ? %{$_[0]} : @_;
  0         0  
51 2 50       9 my $ttl = exists $args{ttl} ? delete $args{ttl} : 5;
52 2 50       8 my $negative_ttl = exists $args{negative_ttl} ? delete $args{negative_ttl} : 1;
53 2 100       25 my $cache = exists $args{cache} ? delete $args{cache} : Cache::Memory::Simple->new;
54              
55 2         12 my $old = $AnyEvent::DNS::RESOLVER;
56 2         4 $AnyEvent::DNS::RESOLVER = do {
57 4     4   28 no warnings 'uninitialized';
  4         14  
  4         755  
58 2   50     53 my $resolver = AnyEvent::DNS::Cache::Simple->new(
59             untaint => 1,
60             max_outstanding => $ENV{PERL_ANYEVENT_MAX_OUTSTANDING_DNS}*1 || 1,
61             adcs_ttl => $ttl,
62             adcs_negative_ttl => $negative_ttl,
63             adcs_cache => $cache,
64             %args
65             );
66 2 100       18683 if ( !$args{server} ) {
67 1 50       12 $ENV{PERL_ANYEVENT_RESOLV_CONF}
68             ? $resolver->_load_resolv_conf_file ($ENV{PERL_ANYEVENT_RESOLV_CONF})
69             : $resolver->os_config;
70             }
71 2         11251 $resolver;
72             };
73             AnyEvent::Util::guard {
74 2     2   2946 $AnyEvent::DNS::RESOLVER = $old;
75 2         36 };
76             }
77              
78             1;
79             __END__