Class: Higgs::LRUCache
- Inherits:
-
Object
- Object
- Higgs::LRUCache
- Extended by:
- Forwardable
- Defined in:
- lib/higgs/cache.rb
Overview
cache by Least Recently Used strategy
Constant Summary collapse
- CVS_ID =
for ident(1)
'$Id: cache.rb 841 2008-12-24 09:23:20Z toki $'
Instance Attribute Summary collapse
-
#limit_size ⇒ Object
readonly
Returns the value of attribute limit_size.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete(key) ⇒ Object
-
#initialize(limit_size = 1000) ⇒ LRUCache
constructor
A new instance of LRUCache.
Constructor Details
#initialize(limit_size = 1000) ⇒ LRUCache
Returns a new instance of LRUCache.
23 24 25 26 27 |
# File 'lib/higgs/cache.rb', line 23 def initialize(limit_size=1000) @limit_size = limit_size @count = 0 @cache = {} end |
Instance Attribute Details
#limit_size ⇒ Object (readonly)
Returns the value of attribute limit_size.
29 30 31 |
# File 'lib/higgs/cache.rb', line 29 def limit_size @limit_size end |
Instance Method Details
#[](key) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/higgs/cache.rb', line 35 def [](key) if (cached_pair = @cache[key]) then cached_pair[1] = @count @count = @count.succ return cached_pair[0] end nil end |
#[]=(key, value) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/higgs/cache.rb', line 44 def []=(key, value) if (cached_pair = @cache[key]) then cached_pair[1] = @count else @cache[key] = [ value, @count ] end @count = @count.succ if (@cache.size > @limit_size) then purge_old_cache end value end |
#delete(key) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/higgs/cache.rb', line 65 def delete(key) if (cached_pair = @cache.delete(key)) then return cached_pair[0] end nil end |