diff --git a/lib/Zonemaster/Engine/Nameserver/Cache/RedisCache.pm b/lib/Zonemaster/Engine/Nameserver/Cache/RedisCache.pm index e2a9bfe25..9111f8f02 100644 --- a/lib/Zonemaster/Engine/Nameserver/Cache/RedisCache.pm +++ b/lib/Zonemaster/Engine/Nameserver/Cache/RedisCache.pm @@ -66,8 +66,14 @@ sub set_key { my ( $self, $hash, $packet ) = @_; my $key = "ns:" . $self->address . ":" . $hash; + # Never cache with answer, NXDOMAIN or NODATA longer than this. my $redis_expire = $self->{config}->{expire}; - my $ttl = $redis_expire; + + # If no response or response without answer or SOA in authority, + # cache this many seconds (e.g. SERVFAIL or REFUSED). + my $ttl_no_response = 1200; + + my $ttl; $self->data->{$hash} = $packet; if ( defined $packet ) { @@ -90,12 +96,18 @@ sub set_key { } } } - $ttl = $ttl < $redis_expire ? $ttl : $redis_expire; - # Redis requires cache time to be greater than 0. - $ttl = 1 if $ttl == 0; + + if ( defined( $ttl ) ) { + $ttl = $ttl < $redis_expire ? $ttl : $redis_expire; + } else { + $ttl = $ttl_no_response; + } + + # Redis requires cache time to be greater than 0 to be stored. + return if $ttl == 0; $self->redis->set( $key, $msg, 'EX', $ttl ); } else { - $self->redis->set( $key, '', 'EX', $ttl ); + $self->redis->set( $key, '', 'EX', $ttl_no_response ); } } @@ -135,7 +147,7 @@ Zonemaster::Engine::Nameserver::Cache::RedisCache - global shared caches for nam =head1 SYNOPSIS - This is an EXPERIMENTAL caching layer and might change in the future. + This is a global caching layer. =head1 ATTRIBUTES @@ -159,4 +171,12 @@ Retrieve C<$packet> (data) at key C<$idx>. =back +Cache time is the shortest time of TTL in the DNS packet +and cache.redis.expire in the profile. Default value of +cache.redis.expire is 300 seconds. + +If there is no TTL value to be used in the DNS packet +(e.g. SERVFAIL or no response), then cache time is fixed +to 1200 seconds instead. + =cut diff --git a/lib/Zonemaster/Engine/Profile.pm b/lib/Zonemaster/Engine/Profile.pm index 5b00c8153..6d3ad0b4e 100644 --- a/lib/Zonemaster/Engine/Profile.pm +++ b/lib/Zonemaster/Engine/Profile.pm @@ -737,9 +737,9 @@ item in the list will be used, the rest are backups in case the previous ones di Default C<{Cymru: [ "asnlookup.zonemaster.net", "asn.cymru.com" ], RIPE: [ "riswhois.ripe.net" ]}>. -=head2 cache (EXPERIMENTAL) +=head2 cache -A hash of hashes. The currently supported keys are C<"redis">. +A hash of hashes. The currently supported key is C<"redis">. Default C<{}>. =head3 redis @@ -750,7 +750,8 @@ Specifies the address of the Redis server used to perform global caching (C) and an optional expire time (C). C must be a string in the form C. -C must be a non-negative integer and defines a time in seconds. Default 5 seconds. +C must be a non-negative integer and defines a time in seconds. +Default is 300 seconds. =head2 logfilter