Class: DNSTraverse::CachingResolver

Inherits:
Dnsruby::Resolver
  • Object
show all
Defined in:
lib/dnstraverse/caching_resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CachingResolver

Returns a new instance of CachingResolver.



26
27
28
29
30
31
# File 'lib/dnstraverse/caching_resolver.rb', line 26

def initialize(*args)
  @cache = Hash.new
  @requests = 0
  @cache_hits = 0
  super(*args)
end

Instance Attribute Details

#cache_hitsObject (readonly)

Returns the value of attribute cache_hits.



24
25
26
# File 'lib/dnstraverse/caching_resolver.rb', line 24

def cache_hits
  @cache_hits
end

#requestsObject (readonly)

Returns the value of attribute requests.



24
25
26
# File 'lib/dnstraverse/caching_resolver.rb', line 24

def requests
  @requests
end

Instance Method Details

#query(name, type, klass = Dnsruby::Classes.IN) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dnstraverse/caching_resolver.rb', line 33

def query(name, type, klass = Dnsruby::Classes.IN)
  @requests+= 1
  ip = self.config.nameserver[0]
  udp_size = self.udp_size
  Log.debug { "Querying #{name} to #{ip} class #{klass} type #{type}"}
  key = "key:res:#{ip}:#{name}:#{klass}:#{type}:#{udp_size}"
  if @cache.has_key?(key) then
    Log.debug { "Cache hit: #{key}" }
    @cache_hits+= 1
    return @cache[key]
  end
  answer = nil
  begin
    msg = Dnsruby::Message.new
    msg.add_question(name, type, klass)
    msg.add_additional(Dnsruby::RR::OPT.new(udp_size)) if udp_size > 512
    result, error = send_plain_message(msg)
    answer = result || error
  rescue => e
    answer = RuntimeError.new "Dnsruby failure: " + e.to_s
  end  
  @cache[key] = answer
  Log.debug { "Cache store: #{key}" }
  @cache[key]
end