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
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
#expires ⇒ Object
Returns the value of attribute expires.
166 167 168 |
# File 'lib/resolve/hostname.rb', line 166 def expires @expires end |
#mutex ⇒ Object
Returns the value of attribute mutex.
166 167 168 |
# File 'lib/resolve/hostname.rb', line 166 def mutex @mutex end |
#value ⇒ Object
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_refresh ⇒ Object
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 |