Class: LRUCache
Overview
LRUCache
A cache utilizing a simple LRU (Least Recently Used) policy. The items managed by this cache must respond to the #key method. Attempts to optimize reads rather than inserts!
LRU semantics are enforced by inserting the items in a queue. The lru item is always at the tail. Two special sentinels (head, tail) are used to simplify (?) the code.
Defined Under Namespace
Modules: Item Classes: Sentinel
Instance Attribute Summary collapse
-
#head ⇒ Object
readonly
the head sentinel and the tail sentinel, tail.prev points to the lru item.
-
#max_items ⇒ Object
the maximum number of items in the cache.
-
#tail ⇒ Object
readonly
the head sentinel and the tail sentinel, tail.prev points to the lru item.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Lookup an item in the cache.
-
#[]=(key, item) ⇒ Object
The inserted item is considered mru!.
-
#clear ⇒ Object
Clear the cache.
-
#delete(key) ⇒ Object
Delete an item from the cache.
-
#first ⇒ Object
The first (mru) element in the cache.
-
#initialize(max_items) ⇒ LRUCache
constructor
A new instance of LRUCache.
-
#last ⇒ Object
(also: #lru)
The last (lru) element in the cache.
Methods inherited from Hash
#&, #*, #+, #-, #<<, #alias!, #argumentize, autonew, #delete_unless, #delete_values, #delete_values_at, #diff, #each_with_key, #except, #except!, #has_keys?, #has_only_keys?, #insert, #inverse, #join, #mash!, #normalize_keys, #normalize_keys!, #pairs_at, #rand_key, #rand_key!, #rand_pair, #rand_pair!, #rand_value, #rand_value!, #rekey, #rekey!, #replace_each, #restore_snapshot, #reverse_merge, #reverse_merge!, #select!, #shuffle, #shuffle!, #slice, #slice!, #stringify_keys, #stringify_keys!, #swap!, #swapkey!, #symbolize_keys, #symbolize_keys!, #take_snapshot, #to_console, #to_h, #to_openobject, #to_ostruct, #to_ostruct_recurse, #to_proc, #to_proc_with_reponse, #to_struct, #traverse, #traverse!, #update_each, #update_keys, #update_values, #variablize_keys, #variablize_keys!, #weave, zipnew, #|
Constructor Details
#initialize(max_items) ⇒ LRUCache
Returns a new instance of LRUCache.
58 59 60 61 |
# File 'lib/more/facets/lrucache.rb', line 58 def initialize(max_items) @max_items = max_items lru_clear() end |
Instance Attribute Details
#head ⇒ Object (readonly)
the head sentinel and the tail sentinel, tail.prev points to the lru item.
56 57 58 |
# File 'lib/more/facets/lrucache.rb', line 56 def head @head end |
#max_items ⇒ Object
the maximum number of items in the cache.
52 53 54 |
# File 'lib/more/facets/lrucache.rb', line 52 def max_items @max_items end |
#tail ⇒ Object (readonly)
the head sentinel and the tail sentinel, tail.prev points to the lru item.
56 57 58 |
# File 'lib/more/facets/lrucache.rb', line 56 def tail @tail end |
Instance Method Details
#[](key) ⇒ Object
Lookup an item in the cache.
65 66 67 68 69 |
# File 'lib/more/facets/lrucache.rb', line 65 def [](key) if item = super return lru_touch(item) end end |
#[]=(key, item) ⇒ Object
The inserted item is considered mru!
73 74 75 76 77 |
# File 'lib/more/facets/lrucache.rb', line 73 def []=(key, item) item = super item.lru_key = key lru_insert(item) end |
#clear ⇒ Object
Clear the cache.
89 90 91 92 |
# File 'lib/more/facets/lrucache.rb', line 89 def clear super lru_clear() end |
#delete(key) ⇒ Object
Delete an item from the cache.
81 82 83 84 85 |
# File 'lib/more/facets/lrucache.rb', line 81 def delete(key) if item = super lru_delete(item) end end |
#first ⇒ Object
The first (mru) element in the cache.
96 97 98 |
# File 'lib/more/facets/lrucache.rb', line 96 def first @head.lru_next end |
#last ⇒ Object Also known as: lru
The last (lru) element in the cache.
102 103 104 |
# File 'lib/more/facets/lrucache.rb', line 102 def last @tail.lru_prev end |