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.



228
229
230
231
232
# File 'lib/dnsruby/recursor.rb', line 228

def initialize(*args)
  @hash = Hash.new # stores addresses against their expiration
  @fresh = {} # Zero-TTL addresses belong only to the current lookup.
  @mutex = Mutex.new # This class is thread-safe
end

Instance Method Details

#current_values ⇒ Object



251
252
253
# File 'lib/dnsruby/recursor.rb', line 251

def current_values
  @mutex.synchronize { expire; @hash.keys | @fresh.keys }
end

#each ⇒ Object



272
273
274
275
276
# File 'lib/dnsruby/recursor.rb', line 272

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

#length ⇒ Object



269
270
271
# File 'lib/dnsruby/recursor.rb', line 269

def length
  values.length
end

#push(item) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/dnsruby/recursor.rb', line 233

def push(item)
  address, ttl = item
  expiration = Time.now + ttl
  @mutex.synchronize {
    @hash.delete(address)
    @fresh.delete(address)
    if ttl <= 0
      @fresh[address] = true
    else
      @hash[address] = expiration
    end
    @hash.shift while @hash.length > ADDRESS_CACHE_LIMIT
    @fresh.shift while @fresh.length > ADDRESS_CACHE_LIMIT
  }
end

#snapshot(include_current: false) ⇒ Object

Shared caches contain only reusable addresses. Copies also keep later updates to one lookup's working map out of another lookup's view.



256
257
258
259
260
261
262
263
264
# File 'lib/dnsruby/recursor.rb', line 256

def snapshot(include_current: false)
  copy = self.class.new
  @mutex.synchronize do
    expire
    copy.instance_variable_set(:@hash, @hash.dup)
    copy.instance_variable_set(:@fresh, @fresh.dup) if include_current
  end
  copy
end

#values ⇒ Object



248
249
250
# File 'lib/dnsruby/recursor.rb', line 248

def values
  @mutex.synchronize { expire; @hash.keys }
end