Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Finished caching results of AnyEvent::DNS::a

  • Loading branch information...
commit cde4c12c71322f2feabae2a8b7aca753e2dad3ca 1 parent 1083301
@despairr despairr authored
Showing with 15 additions and 4 deletions.
  1. +15 −4 benchmark.pl
View
19 benchmark.pl
@@ -29,19 +29,30 @@
# Caching results of AnyEvent::DNS::a
my $orig_anyeventdnsa = \&AnyEvent::DNS::a;
-my %cache;
+my %result_cache;
+my %callback_cache;
*AnyEvent::DNS::a = sub($$) {
my ($domain, $cb) = @_;
- if ($cache{$domain}) {
- $cb->( @{ $cache{$domain} } );
+ if ($result_cache{$domain}) {
+ $cb->( @{ $result_cache{$domain} } );
return;
}
+ if ($callback_cache{$domain}) {
+ push @{ $callback_cache{$domain} }, $cb;
+ return;
+ }
+
+ $callback_cache{$domain} = [];
+
$orig_anyeventdnsa->( $domain,
sub {
- $cache{$domain} = [ @_ ];
+ $result_cache{$domain} = [ @_ ];
$cb->( @_ );
+ while ( my $cached_cb = shift @{ $callback_cache{$domain} } ) {
+ $cached_cb->( @_ );
+ }
}
);
Please sign in to comment.
Something went wrong with that request. Please try again.