Class: Dnsruby::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/Dnsruby/Cache.rb

Overview

:nodoc: all

Defined Under Namespace

Classes: CacheData, CacheKey

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



32
33
34
35
# File 'lib/Dnsruby/Cache.rb', line 32

def initialize()
@cache = Hash.new
@mutex = Mutex.new
end

Class Method Details

.delete(qname, qtype, qclass = Classes.IN) ⇒ Object



80
81
82
83
84
85
# File 'lib/Dnsruby/Cache.rb', line 80

def Cache.delete(qname, qtype, qclass = Classes.IN)
  key = CacheKey.new(qname, qtype, qclass)
  @mutex.synchronize {
    @cache.delete(key)
  }
end

Instance Method Details

#add(message) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/Dnsruby/Cache.rb', line 44

def add(message)
  q = message.question[0]
  key = CacheKey.new(q.qname, q.qtype, q.qclass).to_s
  data = CacheData.new(message)
  @mutex.synchronize {
    if (@cache[key])
      TheLog.debug("CACHE REPLACE : #{q.qname}, #{q.qtype}\n")
    else
      TheLog.debug("CACHE ADD : #{q.qname}, #{q.qtype}\n")
    end
    @cache[key] = data
  }
end

#cacheObject



36
37
38
# File 'lib/Dnsruby/Cache.rb', line 36

def cache
  @cache
end

#clearObject



39
40
41
42
43
# File 'lib/Dnsruby/Cache.rb', line 39

def clear()
  @mutex.synchronize {
    @cache = Hash.new
  }
end

#find(qname, qtype, qclass = Classes.IN) ⇒ Object

This method “fixes up” the response, so that the header and ttls are OK The resolver will still need to copy the flags and ID across from the query



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/Dnsruby/Cache.rb', line 59

def find(qname, qtype, qclass = Classes.IN)
#      print "CACHE find : #{qname}, #{qtype}\n"

  qn = Name.create(qname)
  qn.absolute = true
  key = CacheKey.new(qn, qtype, qclass).to_s
  @mutex.synchronize {
    data = @cache[key]
    if (!data)
#          print "CACHE lookup failed\n"

      return nil
    end
    if (data.expiration <= Time.now.to_i)
      @cache.delete(key)
      TheLog.debug("CACHE lookup stale\n")
      return nil
    end
    m = data.message
    TheLog.debug("CACHE found\n")
    return m
  }
end