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



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

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.



166
167
168
# File 'lib/resolve/hostname.rb', line 166

def expires
  @expires
end

#mutexObject

Returns the value of attribute mutex.



166
167
168
# File 'lib/resolve/hostname.rb', line 166

def mutex
  @mutex
end

#valueObject

Returns the value of attribute value.



166
167
168
# File 'lib/resolve/hostname.rb', line 166

def value
  @value
end

Instance Method Details

#get_or_refreshObject



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/resolve/hostname.rb', line 176

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