Permalink
Browse files

Import

  • Loading branch information...
0 parents commit b9d933b9bd3a36342e2dc95deaa7857fa651d69d @potyl committed Jan 26, 2011
Showing with 216 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +45 −0 Build.PL
  3. +2 −0 Changes
  4. +6 −0 MANIFEST
  5. +6 −0 MANIFEST.SKIP
  6. +40 −0 README
  7. +111 −0 lib/AnyEvent/CacheDNS.pm
@@ -0,0 +1,6 @@
+Build
+META.yml
+MYMETA.yml
+AnyEvent-CacheDNS-*.tar.gz
+_build
+blib
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Module::Build;
+use File::Spec::Functions;
+
+
+my %requires = (
+ perl => '5.6.1',
+ 'AnyEvent' => 0,
+);
+
+my %configure_requires = (
+ 'File::Spec::Functions' => 0,
+);
+
+my $url_resources = 'http://github.com/potyl/perl-AnyEvent-CacheDNS';
+my $build = Module::Build->new(
+ module_name => 'AnyEvent::CacheDNS',
+ license => 'perl',
+ dist_author => 'Emmanuel Rodriguez <potyl@cpan.org>',
+ dist_version_from => catfile('lib', 'AnyEvent', 'CacheDNS.pm'),
+
+ # Dependencies
+ requires => \%requires,
+
+ configure_requires => \%configure_requires,
+
+ build_requires => {
+ %configure_requires,
+ },
+
+ meta_merge => {
+ resources => {
+ homepage => $url_resources,
+ bugtracker => "$url_resources/issues",
+ repository => $url_resources,
+ },
+ },
+);
+
+
+$build->create_build_script();
@@ -0,0 +1,2 @@
+0.01 - Wed Jan 26 21:31:29 CET 2011
+ - First version
@@ -0,0 +1,6 @@
+Build.PL
+lib/AnyEvent/CacheDNS.pm
+Changes
+MANIFEST This list of files
+MANIFEST.SKIP
+META.yml
@@ -0,0 +1,6 @@
+Build
+_build
+^MYMETA\.yml$
+^\.git/
+^\.gitignore$
+blib
40 README
@@ -0,0 +1,40 @@
+AnyEvent::CacheDNS
+==================
+*******************************************************************************
+This CPAN package provides a simple DNS resolver that caches the results for a
+faster retrieval for subsequent calls.
+
+This is module is very basic and has no policies for clearing our invalidating
+the DNS cache. Patches and suggestions are welcome.
+
+INSTALLATION
+
+To install this module type the following:
+
+ perl Build.PL
+ ./Build
+ ./Build test
+ ./Build install
+
+PROJECT
+
+The project is now hosted on github (http://github.com/potyl/perl-AnyEvent-CacheDNS/)
+which provides the latest source code and a simple bug tracking.
+
+Even tough github provides a download facility, the project's source code
+bundles will always be published through CPAN. It's easier this way for the
+project and after all this is a Perl project!
+
+BUGS
+
+Please when possible try to submit the bugs through the github issue tracker
+(http://github.com/potyl/perl-AnyEvent-CacheDNS/issues) otherwise simply create a
+ticket through RT.
+
+COPYRIGHT AND LICENCE
+
+Copyright (C) 2011 by Emmanuel Rodriguez
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself, either Perl version 5.8.8 or,
+at your option, any later version of Perl 5 you may have available.
@@ -0,0 +1,111 @@
+package AnyEvent::CacheDNS;
+
+use strict;
+use warnings;
+use base 'AnyEvent::DNS';
+
+use Data::Dumper;
+
+our $VERSION = '0.01';
+
+sub import {
+ my $package = shift;
+ my @options = @_;
+
+ while (@options) {
+ my $key = shift @options;
+ if ($key eq ':register') {
+ $package->register();
+ }
+ }
+}
+
+
+sub new {
+ my $class = shift;
+ my $self = $class->SUPER::new(@_);
+ $self->{_cache} = {};
+ return $self;
+}
+
+
+sub resolve {
+ my $cb = pop @_;
+ my ($self, $qname, $qtype, %opt) = @_;
+
+ # If we have the value cached then we serve it from there
+ my $cache = $self->{_cache}{$qtype} ||= {};
+ if (exists $cache->{$qname}) {
+ $cb->($cache->{$qname} ? ($cache->{$qname}) : ());
+ return;
+ }
+
+ # Performe a request and cache the value
+print "request $qname $qtype\n";
+ $self->SUPER::resolve(
+ $qname,
+ $qtype,
+ %opt,
+ sub{
+ # Note that the first time multiple queries could be done to the
+ # same arguments if the value is not cached already. This is why we
+ # assign a value only if we don't have a good value yet.
+ $cache->{$qname} ||= @_ ? $_[0] : undef;
+ $cb->(@_);
+ }
+ );
+}
+
+
+sub register {
+ my $class = shift;
+ $AnyEvent::DNS::RESOLVER = $class->new();
+}
+
+
+1;
+
+=head1 NAME
+
+AnyEvent::CacheDNS - Simple DNS resolver with caching
+
+=head1 AUTHOR
+
+Emmanuel Rodriguez <potyl@cpan.org>
+
+
+=head1 SYNOPSIS
+
+ use AnyEvent::Impl::Perl;
+ use AnyEvent;
+ use AnyEvent::HTTP;
+
+ # Register our DNS resolver as the default resolver
+ use AnyEvent::CacheDNS ':register';
+
+ # Use AnyEvent as ususal
+ my $cond = AnyEvent->condvar;
+ http_get "http://search.cpan.org/", sub { $cond->send(); };
+ $cond->recv();
+
+=head1 DESCRIPTION
+
+This module provides a very simple DNS resolver that caches its results and can
+improve the connection times to remote hosts.
+
+=head1 Import
+
+It's possible to register the this class as AnyEvent's main DNS resolver by
+passing the tag C<:register> in the C<use> statement.
+
+=head1 METHODS
+
+=head2 register
+
+Registers a new DNS cache instance as AnyEvent's global DNS resolver.
+
+=head1 COPYRIGHT
+
+(C) 2011 Emmanuel Rodriguez - All Rights Reserved.
+
+=cut

0 comments on commit b9d933b

Please sign in to comment.