Class: Whoosh::AI::LRUCache

Inherits:
Object
  • Object
show all
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

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

#sizeObject



31
32
33
# File 'lib/whoosh/ai/llm.rb', line 31

def size
  @store.size
end