Class: Resolve::Hostname::CachedValue
- Inherits:
-
Object
- Object
- Resolve::Hostname::CachedValue
- Defined in:
- lib/resolve/hostname.rb
Instance Attribute Summary collapse
-
#expires ⇒ Object
Returns the value of attribute expires.
-
#mutex ⇒ Object
Returns the value of attribute mutex.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #get_or_refresh ⇒ Object
-
#initialize(ttl) ⇒ CachedValue
constructor
TODO: negative cache.
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
#expires ⇒ Object
Returns the value of attribute expires.
172 173 174 |
# File 'lib/resolve/hostname.rb', line 172 def expires @expires end |
#mutex ⇒ Object
Returns the value of attribute mutex.
172 173 174 |
# File 'lib/resolve/hostname.rb', line 172 def mutex @mutex end |
#value ⇒ Object
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_refresh ⇒ Object
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 |