Class: LruCache
- Inherits:
-
Object
- Object
- LruCache
- Defined in:
- lib/kotoshu/embeddings/lru_cache.rb
Overview
LruCache - Least Recently Used Cache
Provides efficient O(1) LRU caching with optional TTL support. Used for caching embeddings during similarity search.
Instance Attribute Summary collapse
-
#hits ⇒ Integer
readonly
Number of cache hits.
-
#max_size ⇒ Integer
readonly
Maximum number of entries.
-
#misses ⇒ Integer
readonly
Number of cache misses.
-
#ttl ⇒ Integer?
readonly
TTL in seconds.
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Get value for key.
-
#[]=(key, value) ⇒ Object
Set value for key.
-
#clear ⇒ self
Clear all entries.
-
#delete(key) ⇒ Object?
Delete key from cache.
-
#empty? ⇒ Boolean
Check if empty.
-
#fetch(key, &block) ⇒ Object
Fetch with block (cache-aside pattern).
-
#initialize(max_size: 1000, ttl: nil) ⇒ LruCache
constructor
Create a new LRU cache.
-
#key?(key) ⇒ Boolean
Check if key exists.
-
#keys ⇒ Array<Object>
Get all keys.
-
#lru ⇒ Array<Object, Object>?
Get least recently used key-value pair.
-
#mru ⇒ Array<Object, Object>?
Get most recently used key-value pair.
-
#size ⇒ Integer
Get current size.
-
#stats ⇒ Hash
Get cache statistics.
-
#values ⇒ Array<Object>
Get all values.
Constructor Details
#initialize(max_size: 1000, ttl: nil) ⇒ LruCache
Create a new LRU cache
34 35 36 37 38 39 40 41 |
# File 'lib/kotoshu/embeddings/lru_cache.rb', line 34 def initialize(max_size: 1000, ttl: nil) @max_size = max_size @ttl = ttl @cache = {} # key -> {value: v, accessed_at: t, created_at: t} @order = [] # Ordered list of keys (most recently used first) @hits = 0 @misses = 0 end |
Instance Attribute Details
#hits ⇒ Integer (readonly)
Returns Number of cache hits.
24 25 26 |
# File 'lib/kotoshu/embeddings/lru_cache.rb', line 24 def hits @hits end |
#max_size ⇒ Integer (readonly)
Returns Maximum number of entries.
18 19 20 |
# File 'lib/kotoshu/embeddings/lru_cache.rb', line 18 def max_size @max_size end |
#misses ⇒ Integer (readonly)
Returns Number of cache misses.
27 28 29 |
# File 'lib/kotoshu/embeddings/lru_cache.rb', line 27 def misses @misses end |
#ttl ⇒ Integer? (readonly)
Returns TTL in seconds.
21 22 23 |
# File 'lib/kotoshu/embeddings/lru_cache.rb', line 21 def ttl @ttl end |
Instance Method Details
#[](key) ⇒ Object?
Get value for key
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/kotoshu/embeddings/lru_cache.rb', line 48 def [](key) entry = @cache[key] return nil unless entry # Check TTL if @ttl && (Time.now - entry[:created_at]) > @ttl delete(key) @misses += 1 return nil end # Update access order (move to front = most recently used) @order.delete(key) @order.unshift(key) entry[:accessed_at] = Time.now @hits += 1 entry[:value] end |
#[]=(key, value) ⇒ Object
Set value for key
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/kotoshu/embeddings/lru_cache.rb', line 74 def []=(key, value) # Evict LRU if at capacity if @cache.key?(key) # Update existing entry @cache[key][:value] = value @cache[key][:accessed_at] = Time.now # Move to front @order.delete(key) @order.unshift(key) return value end if @cache.size >= @max_size evict_lru end @cache[key] = { value: value, accessed_at: Time.now, created_at: Time.now } @order.unshift(key) value end |
#clear ⇒ self
Clear all entries
132 133 134 135 136 |
# File 'lib/kotoshu/embeddings/lru_cache.rb', line 132 def clear @cache.clear @order.clear self end |
#delete(key) ⇒ Object?
Delete key from cache
122 123 124 125 126 |
# File 'lib/kotoshu/embeddings/lru_cache.rb', line 122 def delete(key) entry = @cache.delete(key) @order.delete(key) entry&.[](:value) end |
#empty? ⇒ Boolean
Check if empty
150 151 152 |
# File 'lib/kotoshu/embeddings/lru_cache.rb', line 150 def empty? @cache.empty? end |
#fetch(key, &block) ⇒ Object
Fetch with block (cache-aside pattern)
213 214 215 216 217 218 219 220 |
# File 'lib/kotoshu/embeddings/lru_cache.rb', line 213 def fetch(key, &block) result = self[key] return result if result || key?(key) value = block.call self[key] = value value end |
#key?(key) ⇒ Boolean
Check if key exists
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/kotoshu/embeddings/lru_cache.rb', line 105 def key?(key) entry = @cache[key] return false unless entry if @ttl && (Time.now - entry[:created_at]) > @ttl delete(key) return false end true end |
#keys ⇒ Array<Object>
Get all keys
180 181 182 |
# File 'lib/kotoshu/embeddings/lru_cache.rb', line 180 def keys @order.dup end |
#lru ⇒ Array<Object, Object>?
Get least recently used key-value pair
158 159 160 161 162 163 |
# File 'lib/kotoshu/embeddings/lru_cache.rb', line 158 def lru return nil if @order.empty? key = @order.last [key, @cache[key][:value]] end |
#mru ⇒ Array<Object, Object>?
Get most recently used key-value pair
169 170 171 172 173 174 |
# File 'lib/kotoshu/embeddings/lru_cache.rb', line 169 def mru return nil if @order.empty? key = @order.first [key, @cache[key][:value]] end |
#size ⇒ Integer
Get current size
142 143 144 |
# File 'lib/kotoshu/embeddings/lru_cache.rb', line 142 def size @cache.size end |
#stats ⇒ Hash
Get cache statistics
196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/kotoshu/embeddings/lru_cache.rb', line 196 def stats total = @hits + @misses { size: size, max_size: @max_size, hits: @hits, misses: @misses, hit_rate: total.zero? ? 0.0 : @hits.to_f / total, ttl: @ttl } end |
#values ⇒ Array<Object>
Get all values
188 189 190 |
# File 'lib/kotoshu/embeddings/lru_cache.rb', line 188 def values @order.map { |key| @cache[key][:value] } end |