Class: PuppetX::Wmflib::DNS::BasicTTLCache

Inherits:
Object
  • Object
show all
Defined in:
modules/wmflib/lib/puppet_x/wmflib/dns.rb

Instance Method Summary collapse

Constructor Details

#initializeBasicTTLCache

Returns a new instance of BasicTTLCache.



36
37
38
# File 'modules/wmflib/lib/puppet_x/wmflib/dns.rb', line 36

def initialize
  @cache = {}
end

Instance Method Details

#delete(key) ⇒ Object



44
45
46
# File 'modules/wmflib/lib/puppet_x/wmflib/dns.rb', line 44

def delete(key)
  @cache.delete(key) if @cache.key?(key)
end

#read(key) ⇒ Object



58
59
60
61
62
63
# File 'modules/wmflib/lib/puppet_x/wmflib/dns.rb', line 58

def read(key)
  if valid?key
    return @cache[key].value
  end
  nil
end

#read_stale(key) ⇒ Object



65
66
67
68
69
70
# File 'modules/wmflib/lib/puppet_x/wmflib/dns.rb', line 65

def read_stale(key)
  if @cache.key?(key)
    return @cache[key].value
  end
  nil
end

#valid?(key) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
# File 'modules/wmflib/lib/puppet_x/wmflib/dns.rb', line 48

def valid?(key)
  # If the key exists, and its ttl has not expired, return true.
  # Return false (and maybe clean up the stale entry) otherwise.
  return false unless @cache.key?(key)
  t = Time.now.to_i
  return true if @cache[key].valid?t

  false
end

#write(key, value, ttl) ⇒ Object



40
41
42
# File 'modules/wmflib/lib/puppet_x/wmflib/dns.rb', line 40

def write(key, value, ttl)
  @cache[key] = CacheEntry.new(value, ttl)
end