Class: VirtualFS::RuntimeCache
- Inherits:
-
Object
- Object
- VirtualFS::RuntimeCache
- Defined in:
- lib/virtualfs/runtime_cache.rb
Instance Attribute Summary collapse
-
#expires_after ⇒ Object
Returns the value of attribute expires_after.
Instance Method Summary collapse
- #cache(key, &proc) ⇒ Object
-
#initialize(opts = {}) ⇒ RuntimeCache
constructor
A new instance of RuntimeCache.
Constructor Details
#initialize(opts = {}) ⇒ RuntimeCache
Returns a new instance of RuntimeCache.
5 6 7 8 9 10 |
# File 'lib/virtualfs/runtime_cache.rb', line 5 def initialize(opts={}) @expires_after = opts[:expires_after] || -1 @store = {} @times = {} end |
Instance Attribute Details
#expires_after ⇒ Object
Returns the value of attribute expires_after.
3 4 5 |
# File 'lib/virtualfs/runtime_cache.rb', line 3 def expires_after @expires_after end |
Instance Method Details
#cache(key, &proc) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/virtualfs/runtime_cache.rb', line 12 def cache(key, &proc) access_time = Time.now # Invalidate cache after some time if (@expires_after > -1) && ((access_time - (@times[key] || access_time)) > @expires_after) @times[key] = access_time @store[key] = nil end @times[key] ||= access_time @store[key] ||= yield end |