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.



116
117
118
119
# File 'lib/dnsruby/recursor.rb', line 116

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

Instance Method Details

#eachObject



149
150
151
152
153
# File 'lib/dnsruby/recursor.rb', line 149

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

#lengthObject



144
145
146
147
148
# File 'lib/dnsruby/recursor.rb', line 144

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

#push(item) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/dnsruby/recursor.rb', line 120

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

#valuesObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/dnsruby/recursor.rb', line 127

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