| 1 | package Zonemaster::Engine::Nameserver::Cache::RedisCache; | ||
| 2 | |||
| 3 | use v5.16.0; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use version; our $VERSION = version->declare("v1.0.0"); | ||
| 7 | |||
| 8 | use Class::Accessor "antlers"; | ||
| 9 | use Time::HiRes qw[gettimeofday tv_interval]; | ||
| 10 | use List::Util qw( min ); | ||
| 11 | |||
| 12 | use Zonemaster::LDNS::Packet; | ||
| 13 | use Zonemaster::Engine::Packet; | ||
| 14 | use Zonemaster::Engine::Profile; | ||
| 15 | |||
| 16 | use base qw( Zonemaster::Engine::Nameserver::Cache ); | ||
| 17 | |||
| 18 | eval { | ||
| 19 | use Data::MessagePack; | ||
| 20 | use Redis; | ||
| 21 | }; | ||
| 22 | |||
| 23 | if ( $@ ) { | ||
| 24 | die "Can't use the Redis cache. Make sure the Data::MessagePack and Redis modules are installed.\n"; | ||
| 25 | } | ||
| 26 | |||
| 27 | my $redis; | ||
| 28 | my $config; | ||
| 29 | our $object_cache = \%Zonemaster::Engine::Nameserver::Cache::object_cache; | ||
| 30 | |||
| 31 | my $REDIS_EXPIRE_DEFAULT = 300; # seconds | ||
| 32 | |||
| 33 | has 'redis' => ( is => 'ro' ); | ||
| 34 | has 'config' => ( is => 'ro' ); | ||
| 35 | |||
| 36 | my $mp = Data::MessagePack->new(); | ||
| 37 | |||
| 38 | sub new { | ||
| 39 | my $proto = shift; | ||
| 40 | my $params = shift; | ||
| 41 | $params->{address} = $params->{address}->ip; | ||
| 42 | if ( exists $object_cache->{ $params->{address} } ) { | ||
| 43 | Zonemaster::Engine->logger->add( CACHE_FETCHED => { ip => $params->{address} } ); | ||
| 44 | return $object_cache->{ $params->{address} }; | ||
| 45 | } else { | ||
| 46 | if (! defined $redis) { | ||
| 47 | my $redis_config = Zonemaster::Engine::Profile->effective->get( q{cache} )->{'redis'}; | ||
| 48 | $redis = Redis->new(server => $redis_config->{server}); | ||
| 49 | $config = $redis_config; | ||
| 50 | } | ||
| 51 | $params->{redis} //= $redis; | ||
| 52 | $params->{data} //= {}; | ||
| 53 | $params->{config} //= $config; | ||
| 54 | $config->{expire} //= $REDIS_EXPIRE_DEFAULT; | ||
| 55 | my $class = ref $proto || $proto; | ||
| 56 | my $obj = Class::Accessor::new( $class, $params ); | ||
| 57 | |||
| 58 | Zonemaster::Engine->logger->add( CACHE_CREATED => { ip => $params->{address} } ); | ||
| 59 | $object_cache->{ $params->{address} } = $obj; | ||
| 60 | |||
| 61 | return $obj; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | sub set_key { | ||
| 66 | my ( $self, $hash, $packet ) = @_; | ||
| 67 | my $key = "ns:" . $self->address . ":" . $hash; | ||
| 68 | |||
| 69 | # Never cache with answer, NXDOMAIN or NODATA longer than this. | ||
| 70 | my $redis_expire = $self->{config}->{expire}; | ||
| 71 | |||
| 72 | # If no response or response without answer or SOA in authority, | ||
| 73 | # cache this many seconds (e.g. SERVFAIL or REFUSED). | ||
| 74 | my $ttl_no_response = 1200; | ||
| 75 | |||
| 76 | my $ttl; | ||
| 77 | |||
| 78 | $self->data->{$hash} = $packet; | ||
| 79 | if ( defined $packet ) { | ||
| 80 | my $msg = $mp->pack({ | ||
| 81 | data => $packet->data, | ||
| 82 | answerfrom => $packet->answerfrom, | ||
| 83 | timestamp => $packet->timestamp, | ||
| 84 | querytime => $packet->querytime, | ||
| 85 | }); | ||
| 86 | if ( $packet->answer ) { | ||
| 87 | my @rr = $packet->answer; | ||
| 88 | $ttl = min( map { $_->ttl } @rr ); | ||
| 89 | } | ||
| 90 | elsif ( $packet->authority ) { | ||
| 91 | my @rr = $packet->authority; | ||
| 92 | foreach my $r (@rr) { | ||
| 93 | if ( $r->type eq 'SOA' ) { | ||
| 94 | $ttl = $r->ttl; | ||
| 95 | last; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | if ( defined( $ttl ) ) { | ||
| 101 | $ttl = $ttl < $redis_expire ? $ttl : $redis_expire; | ||
| 102 | } else { | ||
| 103 | $ttl = $ttl_no_response; | ||
| 104 | } | ||
| 105 | |||
| 106 | # Redis requires cache time to be greater than 0 to be stored. | ||
| 107 | return if $ttl == 0; | ||
| 108 | $self->redis->set( $key, $msg, 'EX', $ttl ); | ||
| 109 | } else { | ||
| 110 | $self->redis->set( $key, '', 'EX', $ttl_no_response ); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | sub get_key { | ||
| 115 | my ( $self, $hash ) = @_; | ||
| 116 | my $key = "ns:" . $self->address . ":" . $hash; | ||
| 117 | |||
| 118 | if ( exists $self->data->{$hash} ) { | ||
| 119 | Zonemaster::Engine->logger->add( MEMORY_CACHE_HIT => { hash => $hash } ); | ||
| 120 | return ( 1, $self->data->{$hash} ); | ||
| 121 | } elsif ( $self->redis->exists($key) ) { | ||
| 122 | my $fetch_start_time = [ gettimeofday ]; | ||
| 123 | my $data = $self->redis->get( $key ); | ||
| 124 | Zonemaster::Engine->logger->add( REDIS_CACHE_HIT => { key => $key } ); | ||
| 125 | if ( not length($data) ) { | ||
| 126 | $self->data->{$hash} = undef; | ||
| 127 | } else { | ||
| 128 | my $msg = $mp->unpack( $data ); | ||
| 129 | my $packet = Zonemaster::Engine::Packet->new({ packet => Zonemaster::LDNS::Packet->new_from_wireformat($msg->{data}) }); | ||
| 130 | $packet->answerfrom( $msg->{answerfrom} ); | ||
| 131 | $packet->timestamp( $msg->{timestamp} ); | ||
| 132 | $packet->querytime( $msg->{querytime} ); | ||
| 133 | |||
| 134 | $self->data->{$hash} = $packet; | ||
| 135 | } | ||
| 136 | return ( 1, $self->data->{$hash} ); | ||
| 137 | } | ||
| 138 | Zonemaster::Engine->logger->add( CACHE_MISS => { key => $key } ); | ||
| 139 | return ( 0, undef ) | ||
| 140 | } | ||
| 141 | |||
| 142 | 1; | ||
| 143 | |||
| 144 | =head1 NAME | ||
| 145 | |||
| 146 | Zonemaster::Engine::Nameserver::Cache::RedisCache - global shared caches for nameserver objects | ||
| 147 | |||
| 148 | =head1 SYNOPSIS | ||
| 149 | |||
| 150 | This is a global caching layer. | ||
| 151 | |||
| 152 | =head1 ATTRIBUTES | ||
| 153 | |||
| 154 | Subclass of L<Zonemaster::Engine::Nameserver::Cache>. | ||
| 155 | |||
| 156 | =head1 CLASS METHODS | ||
| 157 | |||
| 158 | =over | ||
| 159 | |||
| 160 | =item new | ||
| 161 | |||
| 162 | Construct a new Cache object. | ||
| 163 | |||
| 164 | =item set_key($idx, $packet) | ||
| 165 | |||
| 166 | Store C<$packet> with key C<$idx>. | ||
| 167 | |||
| 168 | =item get_key($idx) | ||
| 169 | |||
| 170 | Retrieve C<$packet> (data) at key C<$idx>. | ||
| 171 | |||
| 172 | =back | ||
| 173 | |||
| 174 | Cache time is the shortest time of TTL in the DNS packet | ||
| 175 | and cache.redis.expire in the profile. Default value of | ||
| 176 | cache.redis.expire is 300 seconds. | ||
| 177 | |||
| 178 | If there is no TTL value to be used in the DNS packet | ||
| 179 | (e.g. SERVFAIL or no response), then cache time is fixed | ||
| 180 | to 1200 seconds instead. | ||
| 181 | |||
| 182 | =cut |