Class: BasicTTLCache
- Defined in:
- puppet/modules/wmflib/lib/puppet/parser/functions/ipresolve.rb
Instance Method Summary collapse
- #delete(key) ⇒ Object
-
#initialize ⇒ BasicTTLCache
constructor
A new instance of BasicTTLCache.
- #read(key) ⇒ Object
- #read_stale(key) ⇒ Object
- #valid?(key) ⇒ Boolean
- #write(key, value, ttl) ⇒ Object
Constructor Details
#initialize ⇒ BasicTTLCache
Returns a new instance of BasicTTLCache.
32 33 34 |
# File 'puppet/modules/wmflib/lib/puppet/parser/functions/ipresolve.rb', line 32 def initialize @cache = {} end |
Instance Method Details
#delete(key) ⇒ Object
40 41 42 |
# File 'puppet/modules/wmflib/lib/puppet/parser/functions/ipresolve.rb', line 40 def delete(key) @cache.delete(key) if @cache.key?(key) end |
#read(key) ⇒ Object
54 55 56 57 58 59 |
# File 'puppet/modules/wmflib/lib/puppet/parser/functions/ipresolve.rb', line 54 def read(key) if valid?key return @cache[key].value end nil end |
#read_stale(key) ⇒ Object
61 62 63 64 65 66 |
# File 'puppet/modules/wmflib/lib/puppet/parser/functions/ipresolve.rb', line 61 def read_stale(key) if @cache.key?(key) return @cache[key].value end nil end |
#valid?(key) ⇒ Boolean
44 45 46 47 48 49 50 51 52 |
# File 'puppet/modules/wmflib/lib/puppet/parser/functions/ipresolve.rb', line 44 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
36 37 38 |
# File 'puppet/modules/wmflib/lib/puppet/parser/functions/ipresolve.rb', line 36 def write(key, value, ttl) @cache[key] = DNSCacheEntry.new(value, ttl) end |