Class: HashCache::LRU
- Inherits:
-
Object
- Object
- HashCache::LRU
- Includes:
- Accessible
- Defined in:
- lib/hash_cache/lru.rb
Instance Attribute Summary collapse
-
#max_keys ⇒ Object
Returns the value of attribute max_keys.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
-
#initialize(options = {}) ⇒ LRU
constructor
A new instance of LRU.
- #size ⇒ Object
Methods included from Accessible
Constructor Details
#initialize(options = {}) ⇒ LRU
Returns a new instance of LRU.
9 10 11 12 13 |
# File 'lib/hash_cache/lru.rb', line 9 def initialize( = {}) self.max_keys = .fetch(:max_keys, 20) self.data = {} self.keys = LinkedList.new end |
Instance Attribute Details
#max_keys ⇒ Object
Returns the value of attribute max_keys.
7 8 9 |
# File 'lib/hash_cache/lru.rb', line 7 def max_keys @max_keys end |
Instance Method Details
#[](key) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/hash_cache/lru.rb', line 15 def [](key) return nil unless data.has_key?(key) value, node = data[key] keys.move_to_head(node) value end |
#[]=(key, val) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/hash_cache/lru.rb', line 22 def []=(key, val) if data.has_key?(key) data[key][0] = val else node = keys.prepend(key) data[key] = [val, node] end prune val end |
#size ⇒ Object
33 34 35 |
# File 'lib/hash_cache/lru.rb', line 33 def size keys.length end |