Skip to content

Commit

Permalink
Merge pull request #1394 from matsduf/makes-global-chache-unexperimental
Browse files Browse the repository at this point in the history
Updates global cache
  • Loading branch information
matsduf authored Nov 12, 2024
2 parents 1b8482e + 552d2fc commit e4e89b4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
32 changes: 26 additions & 6 deletions lib/Zonemaster/Engine/Nameserver/Cache/RedisCache.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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 );
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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
7 changes: 4 additions & 3 deletions lib/Zonemaster/Engine/Profile.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -750,7 +750,8 @@ Specifies the address of the Redis server used to perform global caching
(C<cache.redis.server>) and an optional expire time (C<cache.redis.expire>).
C<cache.redis.server> must be a string in the form C<host:port>.
C<cache.redis.expire> must be a non-negative integer and defines a time in seconds. Default 5 seconds.
C<cache.redis.expire> must be a non-negative integer and defines a time in seconds.
Default is 300 seconds.
=head2 logfilter
Expand Down

0 comments on commit e4e89b4

Please sign in to comment.