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



83
84
85
86
87
88
# File 'lib/dnsruby/cache.rb', line 83

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



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dnsruby/cache.rb', line 47

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


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/dnsruby/cache.rb', line 62

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

#lengthObject



44
45
46
# File 'lib/dnsruby/cache.rb', line 44

def length
  return @cache.length
end