Class: Net::Hippie::DnsCache

Inherits:
Object
  • Object
show all
Defined in:
lib/net/hippie/dns_cache.rb

Overview

Thread-safe DNS resolution cache with TTL.

Instance Method Summary collapse

Constructor Details

#initialize(timeout: 5, ttl: 300, logger: nil) ⇒ DnsCache

Returns a new instance of DnsCache.



7
8
9
10
11
12
13
# File 'lib/net/hippie/dns_cache.rb', line 7

def initialize(timeout: 5, ttl: 300, logger: nil)
  @timeout = timeout
  @ttl = ttl
  @logger = logger
  @cache = {}
  @monitor = Monitor.new
end

Instance Method Details

#clearObject



27
28
29
# File 'lib/net/hippie/dns_cache.rb', line 27

def clear
  @monitor.synchronize { @cache.clear }
end

#resolve(hostname) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/net/hippie/dns_cache.rb', line 15

def resolve(hostname)
  cached = get(hostname)
  return cached if cached

  ip = Net::Hippie.resolve(hostname, timeout: @timeout)
  set(hostname, ip)
  ip
rescue Timeout::Error, Resolv::ResolvError => error
  @logger&.warn("[Hippie] DNS resolution failed for #{hostname}: #{error.message}")
  nil
end