| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # (C) Paul Evans, 2014-2015 -- leonerd@leonerd.org.uk | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package IO::Async::Resolver::StupidCache; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 3 | ▶ | use base qw( IO::Async::Notifier ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 3 | our $VERSION = '0.02'; | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 3 | ▶ | use IO::Async::Resolver; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 3 | ▶ | use Struct::Dumb qw( readonly_struct ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 3 | readonly_struct CacheEntry => [qw( future expires )]; | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =head1 NAME | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | C<IO::Async::Resolver::StupidCache> - a trivial caching layer around an C<IO::Async::Resolver> | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | use IO::Async::Loop 0.62; | ||||||||||||||||||||
| 26 | use IO::Async::Resolver::StupidCache; | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | my $loop = IO::Async::Loop->new; | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | # Wrap the existing resolver in a cache | ||||||||||||||||||||
| 31 | $loop->set_resolver( | ||||||||||||||||||||
| 32 | IO::Async::Resolver::StupidCache->new( source => $loop->resolver ) | ||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | # $loop->resolve requests will now be cached | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | =head1 DESCRIPTION | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | This object class provides a wrapper around another L<IO::Async::Resolver> | ||||||||||||||||||||
| 40 | instance, which applies a simple caching layer to avoid making identical | ||||||||||||||||||||
| 41 | lookups. This can be useful, for example, when performing a large number of | ||||||||||||||||||||
| 42 | HTTP requests to the same host or a small set of hosts, or other cases where | ||||||||||||||||||||
| 43 | it is expected that the same few resolver queries will be made over and over. | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | This is called a "stupid" cache because it is made without awareness of TTL | ||||||||||||||||||||
| 46 | values or other cache-relevant information that may be provided by DNS or | ||||||||||||||||||||
| 47 | other resolve methods. As such, it should not be relied upon to give | ||||||||||||||||||||
| 48 | always-accurate answers. | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | =head1 PARAMETERS | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | The following named parameters may be passed to C<new> or C<configure>: | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =over 8 | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | =item source => IO::Async::Resolver | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | Optional. The source of the cache data. If not supplied, a new | ||||||||||||||||||||
| 61 | C<IO::Async::Resolver> instance will be constructed. | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | =item ttl => INT | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | Optional. Time-to-live of cache entries in seconds. If not supplied a default | ||||||||||||||||||||
| 66 | of 5 minutes will apply. | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | =item max_size => INT | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | Optional. Maximum number of entries to keep in the cache. Entries will be | ||||||||||||||||||||
| 71 | evicted at random over this limit. If not supplied a default of 1000 entries | ||||||||||||||||||||
| 72 | will apply. | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | =back | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =cut | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | sub _init | ||||||||||||||||||||
| 79 | { | ||||||||||||||||||||
| 80 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_init: called CC=4 SCAR=15.0
| |||||||||||||||||||||
| 81 | 2 | my ( $params ) = @_; | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 2 | ▶ | $params->{source} ||= IO::Async::Resolver->new; | ||||||||||||||||||
|
Condition33%1/3
$params->{"source"} ||= "IO::Async::Resolver"->new
MC/DC0%0/2
$params->{"source"} ||= "IO::Async::Resolver"->new
$params->{"source"}
"IO::Async::Resolver"->new
Truth table33%1/3
A ||= B
A= $params->{"source"}B= "IO::Async::Resolver"->new
| |||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 2 | ▶ | $params->{ttl} ||= 300; | ||||||||||||||||||
|
Condition50%1/2
$params->{"ttl"} ||= 300
MC/DC0%0/1
$params->{"ttl"} ||= 300
$params->{"ttl"}
Truth table50%1/2
A
A= $params->{"ttl"}
| |||||||||||||||||||||
| 86 | 2 | ▶ | $params->{max_size} ||= 1000; | ||||||||||||||||||
|
Condition50%1/2
$params->{"max_size"} ||= 1000
MC/DC0%0/1
$params->{"max_size"} ||= 1000
$params->{"max_size"}
Truth table50%1/2
A
A= $params->{"max_size"}
| |||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 2 | $self->SUPER::_init( $params ); | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | sub configure | ||||||||||||||||||||
| 92 | { | ||||||||||||||||||||
| 93 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
configure: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||
| 94 | 2 | my %params = @_; | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 2 | foreach (qw( source ttl max_size )) { | |||||||||||||||||||
| 97 | 6 | ▶ | $self->{$_} = delete $params{$_} if exists $params{$_}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 2 | $self->SUPER::configure( %params ); | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | =head1 METHODS | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | The following methods documented with a trailing call to C<< ->get >> return | ||||||||||||||||||||
| 106 | L<Future> instances. | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | =cut | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | =head2 $resolver = $cache->source | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | Returns the source resolver | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | sub source | ||||||||||||||||||||
| 117 | { | ||||||||||||||||||||
| 118 | 3 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
source: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 119 | 3 | return $self->{source}; | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =head2 @result = $cache->resolve( %args )->get | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =head2 @addrs = $cache->getaddrinfo( %args )->get | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =head2 ( $host, $service ) = $cache->getnameinfo( %args )->get | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | These methods perform identically to the base C<IO::Async::Resolver> class, | ||||||||||||||||||||
| 129 | except that the results are cached. | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | Returned C<Futures> are created with the C<without_cancel> method, so that | ||||||||||||||||||||
| 132 | multiple concurrent waiters are shielded from cancellation by one another. | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | =cut | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | sub resolve | ||||||||||||||||||||
| 137 | { | ||||||||||||||||||||
| 138 | 8 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
resolve: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||
| 139 | 8 | my %args = @_; | |||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 8 | my $type = $args{type}; | |||||||||||||||||||
| 142 | 8 | my $data = $args{data}; | |||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 8 | ▶ | my $cache = $self->{cache} ||= {}; | ||||||||||||||||||
|
Condition100%2/2
$self->{"cache"} ||= {}
MC/DC100%1/1
$self->{"cache"} ||= {}
$self->{"cache"}
Truth table100%2/2
A
A= $self->{"cache"}
| |||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 8 | my $now = $self->loop->time; | |||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | # At the current time, all the resolvers use a flat list of non-ref scalars | ||||||||||||||||||||
| 149 | # as arguments. We can simply flatten this to a string to use as our cache key | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | # getaddrinfo needs special handling as it's a name/value pair list; accept | ||||||||||||||||||||
| 152 | # also getaddrinfo_hash | ||||||||||||||||||||
| 153 | my $cachekey = join "\0", ( $type =~ m/^getaddrinfo(?:_hash)?$/ ) | ||||||||||||||||||||
| 154 | 8 | ▶ | ? do { my %data = @$data; $type, map { $_ => $data{$_} } sort keys %data } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 155 | : ( $type, @$data ); | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 8 | ▶ | if( my $entry = $cache->{$cachekey} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | 5 | ▶ | return $entry->future->without_cancel if $entry->expires > $now; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 3 | my $f = $self->source->resolve( %args ); | |||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 2 | $cache->{$cachekey} = CacheEntry( $f, $now + $self->{ttl} ); | |||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 2 | ▶ | while( scalar( keys %$cache ) > $self->{max_size} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 166 | 0 | delete $cache->{ ( keys %$cache )[rand keys %$cache] }; | |||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 2 | return $f->without_cancel; | |||||||||||||||||||
| 170 | } | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | # Resolver's ->getaddrinfo and ->getnameinfo convenience methods are useful to | ||||||||||||||||||||
| 173 | # have here, but are implemented in terms of the basic ->resolve. | ||||||||||||||||||||
| 174 | # We can cheat and just import those methods directly here | ||||||||||||||||||||
| 175 | 3 | *getaddrinfo = \&IO::Async::Resolver::getaddrinfo; | |||||||||||||||||||
| 176 | 3 | *getnameinfo = \&IO::Async::Resolver::getnameinfo; | |||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =head1 AUTHOR | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | =cut | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 3 | 0x55AA; | |||||||||||||||||||