Class: Dnsruby::Recursor::AddressCache

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

Overview

:nodoc: all

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ AddressCache

Like an array, but stores the expiration of each record.



20
21
22
23
# File 'lib/Dnsruby/Recursor.rb', line 20

def initialize(*args)
  @hash = Hash.new # stores addresses against their expiration
  @mutex = Mutex.new # This class is thread-safe
end

Instance Method Details

#eachObject



53
54
55
56
57
# File 'lib/Dnsruby/Recursor.rb', line 53

def each()
  values.each {|v|
    yield v
  }
end

#lengthObject



48
49
50
51
52
# File 'lib/Dnsruby/Recursor.rb', line 48

def length
  @mutex.synchronize {
    return @hash.length
  }
end

#push(item) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/Dnsruby/Recursor.rb', line 24

def push(item)
  address, ttl = item
  expiration = Time.now + ttl
  @mutex.synchronize {
    @hash[address] = expiration
  }
end

#valuesObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/Dnsruby/Recursor.rb', line 31

def values
  ret =[]
  keys_to_delete = []
  @mutex.synchronize {
    @hash.keys.each {|address|
      if (@hash[address] > Time.now)
        ret.push(address)
      else
        keys_to_delete.push(address)
      end
    }
    keys_to_delete.each {|key|
      @hash.delete(key)
    }
  }
  return ret
end