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
36
# File 'lib/dnsruby/cache.rb', line 32

def initialize()
@cache = Hash.new
@@max_size = 16*1024 # Get this right...
@mutex = Mutex.new
end

Class Method Details

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



91
92
93
94
95
96
# File 'lib/dnsruby/cache.rb', line 91

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

.max_size=(length) ⇒ Object



48
49
50
# File 'lib/dnsruby/cache.rb', line 48

def Cache.max_size=(length)
   @@max_size = length
end

Instance Method Details

#add(message) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dnsruby/cache.rb', line 51

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

    while @cache.size > @@max_size # keep the cache size reasonable
      @cache.shift
    end
  }
end

#cacheObject



37
38
39
# File 'lib/dnsruby/cache.rb', line 37

def cache
  @cache
end

#clearObject



40
41
42
43
44
# File 'lib/dnsruby/cache.rb', line 40

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


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dnsruby/cache.rb', line 70

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



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

def length
  return @cache.length
end