Class: Whoosh::AI::LRUCache
- Inherits:
-
Object
- Object
- Whoosh::AI::LRUCache
- Defined in:
- lib/whoosh/ai/llm.rb
Overview
Bounded LRU cache. Ruby’s Hash preserves insertion order, so we reorder on read (delete+reinsert) and evict the oldest entry when over capacity.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize(max_size) ⇒ LRUCache
constructor
A new instance of LRUCache.
- #size ⇒ Object
Constructor Details
#initialize(max_size) ⇒ LRUCache
Returns a new instance of LRUCache.
8 9 10 11 12 |
# File 'lib/whoosh/ai/llm.rb', line 8 def initialize(max_size) @max_size = max_size @store = {} @mutex = Mutex.new end |
Instance Method Details
#[](key) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/whoosh/ai/llm.rb', line 14 def [](key) @mutex.synchronize do return nil unless @store.key?(key) value = @store.delete(key) @store[key] = value end end |
#[]=(key, value) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/whoosh/ai/llm.rb', line 22 def []=(key, value) @mutex.synchronize do @store.delete(key) if @store.key?(key) @store[key] = value @store.shift while @store.size > @max_size value end end |
#size ⇒ Object
31 32 33 |
# File 'lib/whoosh/ai/llm.rb', line 31 def size @store.size end |