| 1 | package Zonemaster::Engine::Nameserver::Cache::LocalCache; | ||
| 2 | |||
| 3 | use v5.16.0; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use version; our $VERSION = version->declare("v1.0.4"); | ||
| 7 | |||
| 8 | use Carp qw( confess ); | ||
| 9 | use Scalar::Util qw( blessed ); | ||
| 10 | |||
| 11 | use Zonemaster::Engine; | ||
| 12 | use Zonemaster::Engine::Nameserver::Cache; | ||
| 13 | |||
| 14 | use base qw( Zonemaster::Engine::Nameserver::Cache ); | ||
| 15 | |||
| 16 | our $object_cache = \%Zonemaster::Engine::Nameserver::Cache::object_cache; | ||
| 17 | |||
| 18 | sub new { | ||
| 19 | my $proto = shift; | ||
| 20 | my $class = ref $proto || $proto; | ||
| 21 | my $attrs = shift; | ||
| 22 | |||
| 23 | confess "Attribute \(address\) is required" | ||
| 24 | if !exists $attrs->{address}; | ||
| 25 | |||
| 26 | # Type coercions | ||
| 27 | $attrs->{address} = Net::IP::XS->new( $attrs->{address} ) | ||
| 28 | if !blessed $attrs->{address} || !$attrs->{address}->isa( 'Net::IP::XS' ); | ||
| 29 | |||
| 30 | # Type constraint | ||
| 31 | confess "Argument must be coercible into a Net::IP::XS: address" | ||
| 32 | if !$attrs->{address}->isa( 'Net::IP::XS' ); | ||
| 33 | confess "Argument must be a HASHREF: data" | ||
| 34 | if exists $attrs->{data} && ref $attrs->{data} ne 'HASH'; | ||
| 35 | |||
| 36 | # Default value | ||
| 37 | $attrs->{data} //= {}; | ||
| 38 | |||
| 39 | my $ip = $attrs->{address}->ip; | ||
| 40 | if ( exists $object_cache->{ $ip } ) { | ||
| 41 | Zonemaster::Engine->logger->add( CACHE_FETCHED => { ip => $ip } ); | ||
| 42 | return $object_cache->{ $ip }; | ||
| 43 | } | ||
| 44 | |||
| 45 | my $obj = Class::Accessor::new( $class, $attrs ); | ||
| 46 | |||
| 47 | Zonemaster::Engine->logger->add( CACHE_CREATED => { ip => $ip } ); | ||
| 48 | $object_cache->{ $ip } = $obj; | ||
| 49 | |||
| 50 | return $obj; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub set_key { | ||
| 54 | my ( $self, $idx, $packet ) = @_; | ||
| 55 | $self->data->{$idx} = $packet; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub get_key { | ||
| 59 | my ( $self, $idx ) = @_; | ||
| 60 | |||
| 61 | if ( exists $self->data->{$idx} ) { | ||
| 62 | # cache hit | ||
| 63 | return ( 1, $self->data->{$idx} ); | ||
| 64 | } | ||
| 65 | return ( 0, undef ); | ||
| 66 | } | ||
| 67 | |||
| 68 | 1; | ||
| 69 | |||
| 70 | =head1 NAME | ||
| 71 | |||
| 72 | Zonemaster::Engine::Nameserver::LocalCache - local shared caches for nameserver objects | ||
| 73 | |||
| 74 | =head1 SYNOPSIS | ||
| 75 | |||
| 76 | This class should not be used directly. | ||
| 77 | |||
| 78 | =head1 ATTRIBUTES | ||
| 79 | |||
| 80 | Subclass of L<Zonemaster::Engine::Nameserver::Cache>. | ||
| 81 | |||
| 82 | =head1 CLASS METHODS | ||
| 83 | |||
| 84 | =over | ||
| 85 | |||
| 86 | =item new | ||
| 87 | |||
| 88 | Construct a new Cache object. | ||
| 89 | |||
| 90 | =item set_key($idx, $packet) | ||
| 91 | |||
| 92 | Store C<$packet> (data) with key C<$idx>. | ||
| 93 | |||
| 94 | =item get_key($idx) | ||
| 95 | |||
| 96 | Retrieve C<$packet> (data) at key C<$idx>. | ||
| 97 | |||
| 98 | =back | ||
| 99 | |||
| 100 | =cut |