Class: Resolve::Hostname::CachedValue

Inherits:
Object
  • Object
show all
Defined in:
lib/resolve/hostname.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ttl) ⇒ CachedValue

TODO: negative cache



175
176
177
178
179
180
# File 'lib/resolve/hostname.rb', line 175

def initialize(ttl)
  @value = nil
  @ttl = ttl
  @expires = Time.now + ttl
  @mutex = Mutex.new
end

Instance Attribute Details

#expiresObject

Returns the value of attribute expires.



172
173
174
# File 'lib/resolve/hostname.rb', line 172

def expires
  @expires
end

#mutexObject

Returns the value of attribute mutex.



172
173
174
# File 'lib/resolve/hostname.rb', line 172

def mutex
  @mutex
end

#valueObject

Returns the value of attribute value.



172
173
174
# File 'lib/resolve/hostname.rb', line 172

def value
  @value
end

Instance Method Details

#get_or_refreshObject



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/resolve/hostname.rb', line 182

def get_or_refresh
  return @value if @value && @expires >= Time.now

  @mutex.synchronize do
    return @value if @value && @expires >= Time.now

    @value = yield
    # doesn't do negative cache (updating of @expires is passed when something raised above)
    @expires = Time.now + @ttl
  end

  @value
end