Class: Rx::Cache::LRUCache
- Inherits:
-
Object
- Object
- Rx::Cache::LRUCache
- Defined in:
- lib/rx/cache/lru_cache.rb
Instance Method Summary collapse
- #cache(k, expires_in = 60) ⇒ Object
- #get(k) ⇒ Object
-
#initialize ⇒ LRUCache
constructor
A new instance of LRUCache.
- #put(k, v, expires_in = 60) ⇒ Object
Constructor Details
Instance Method Details
#cache(k, expires_in = 60) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/rx/cache/lru_cache.rb', line 14 def cache(k, expires_in = 60) if value = get(k) return value end value = yield put(k, value, expires_in) value end |
#get(k) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/rx/cache/lru_cache.rb', line 24 def get(k) clean! lock.synchronize do map[k] end end |
#put(k, v, expires_in = 60) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/rx/cache/lru_cache.rb', line 32 def put(k, v, expires_in = 60) lock.synchronize do map[k] = v heap << [k, Time.now + expires_in] end end |