The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


AnyEvent::DNS::Cache::Simple - Simple cache for AnyEvent::DNS


    use AnyEvent::DNS::Cache::Simple;

    my $guard = AnyEvent::DNS::Cache::Simple->register(
        ttl => 60,
        negative_ttl => 5,
        timeout => [1,1]
    for my $i ( 1..3 ) {
        my $cv = AE::cv;
        AnyEvent::DNS::a "", sub {
            say join " | ",@_;
    undef $guard;


AnyEvent::DNS::Cache::Simple provides simple cache capability for AnyEvent::DNS

CPAN already has AnyEvent::CacheDNS module. It also provides simple cache. AnyEvent::DNS::Cache::Simple support ttl, negative_ttl and can use with any cache module. And AnyEvent::DNS::Cache::Simple does not use AnyEvent->timer for purging cache.



Register cache to $AnyEvent::DNS::RESOLVER. This method returns guard object. If the guard object is destroyed, original resolver will be restored

register can accept all AnyEvent::DNS-new> arguments and has some additional arguments.

ttl: Int

maximum positive cache ttl in seconds. (default: 5)

negative_ttl: Int

negative cache ttl in seconds. (default: 1)

cache: Object

Cache object, requires support get, set and remove methods. default: Cache::Memory::Simple is used


AnyEvent::DNS, AnyEvent::Socket, AnyEvent::CacheDNS, Cache::Memory::Simple


Copyright (C) Masahiro Nagano.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


Masahiro Nagano <>