Class: RefreshingCache
- Inherits:
-
Hash
- Object
- Hash
- RefreshingCache
- Defined in:
- lib/refreshing_cache.rb
Constant Summary collapse
- VERSION =
"1.0.0"
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#initialize(timeout: timeout, check_proc: check_proc, value_proc: value_proc) ⇒ RefreshingCache
constructor
A new instance of RefreshingCache.
- #refresh!(key) ⇒ Object
Constructor Details
#initialize(timeout: timeout, check_proc: check_proc, value_proc: value_proc) ⇒ RefreshingCache
Returns a new instance of RefreshingCache.
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/refreshing_cache.rb', line 6 def initialize(timeout: timeout, check_proc: check_proc, value_proc: value_proc) @timeout = timeout @check_proc = check_proc @refresh_proc = value_proc # Stores key => timeout info @timeouts = {} # Actual underlying data we're caching @hash = {} super(@hash) end |
Instance Method Details
#[](key) ⇒ Object
20 21 22 23 |
# File 'lib/refreshing_cache.rb', line 20 def [](key) self[key] = refresh!(key) if regenerate_value?(key) super end |
#refresh!(key) ⇒ Object
25 26 27 28 29 |
# File 'lib/refreshing_cache.rb', line 25 def refresh!(key) val = refresh_proc.call(key, timeouts[key]) timeouts[key] = Time.now val end |