Class: Takagi::EventBus::LRUCache
- Inherits:
-
Object
- Object
- Takagi::EventBus::LRUCache
- Defined in:
- lib/takagi/event_bus/lru_cache.rb
Overview
Thread-safe LRU cache with TTL (pure Ruby) Zero runtime dependencies
Features:
-
Least Recently Used eviction when at capacity
-
Time-To-Live (TTL) based expiration
-
Thread-safe with Mutex
Instance Attribute Summary collapse
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
-
#ttl ⇒ Object
readonly
Returns the value of attribute ttl.
Instance Method Summary collapse
-
#clear ⇒ Object
Clear all entries.
-
#delete(key) ⇒ Object?
Delete entry from cache.
-
#empty? ⇒ Boolean
Check if cache is empty.
-
#get(key) ⇒ Object?
Get value from cache.
-
#initialize(max_size = 1000, ttl = 3600) ⇒ LRUCache
constructor
Initialize LRU cache.
-
#key?(key) ⇒ Boolean
Check if key exists in cache.
-
#keys ⇒ Array
Get all keys in cache.
-
#set(key, value) ⇒ Object
Set value in cache.
-
#size ⇒ Integer
Get number of entries in cache.
-
#stats ⇒ Hash
Get cache statistics.
Constructor Details
#initialize(max_size = 1000, ttl = 3600) ⇒ LRUCache
Initialize LRU cache
24 25 26 27 28 29 30 31 |
# File 'lib/takagi/event_bus/lru_cache.rb', line 24 def initialize(max_size = 1000, ttl = 3600) @max_size = max_size @ttl = ttl @cache = {} @access_order = [] @timestamps = {} @mutex = Mutex.new end |
Instance Attribute Details
#max_size ⇒ Object (readonly)
Returns the value of attribute max_size.
19 20 21 |
# File 'lib/takagi/event_bus/lru_cache.rb', line 19 def max_size @max_size end |
#ttl ⇒ Object (readonly)
Returns the value of attribute ttl.
19 20 21 |
# File 'lib/takagi/event_bus/lru_cache.rb', line 19 def ttl @ttl end |
Instance Method Details
#clear ⇒ Object
Clear all entries
80 81 82 83 84 85 86 |
# File 'lib/takagi/event_bus/lru_cache.rb', line 80 def clear @mutex.synchronize do @cache.clear @access_order.clear @timestamps.clear end end |
#delete(key) ⇒ Object?
Delete entry from cache
71 72 73 74 75 76 77 |
# File 'lib/takagi/event_bus/lru_cache.rb', line 71 def delete(key) @mutex.synchronize do @access_order.delete(key) @timestamps.delete(key) @cache.delete(key) end end |
#empty? ⇒ Boolean
Check if cache is empty
96 97 98 |
# File 'lib/takagi/event_bus/lru_cache.rb', line 96 def empty? @mutex.synchronize { @cache.empty? } end |
#get(key) ⇒ Object?
Get value from cache
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/takagi/event_bus/lru_cache.rb', line 36 def get(key) @mutex.synchronize do cleanup_expired return nil unless @cache.key?(key) # Update access order (move to end = most recently used) @access_order.delete(key) @access_order << key @timestamps[key] = Time.now @cache[key] end end |
#key?(key) ⇒ Boolean
Check if key exists in cache
103 104 105 106 107 108 |
# File 'lib/takagi/event_bus/lru_cache.rb', line 103 def key?(key) @mutex.synchronize do cleanup_expired @cache.key?(key) end end |
#keys ⇒ Array
Get all keys in cache
112 113 114 115 116 117 |
# File 'lib/takagi/event_bus/lru_cache.rb', line 112 def keys @mutex.synchronize do cleanup_expired @cache.keys end end |
#set(key, value) ⇒ Object
Set value in cache
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/takagi/event_bus/lru_cache.rb', line 53 def set(key, value) @mutex.synchronize do cleanup_expired # Remove oldest entry if at capacity and key is new evict_oldest if @cache.size >= @max_size && !@cache.key?(key) # Add/update entry @access_order.delete(key) # Remove if exists @access_order << key # Add to end (most recently used) @cache[key] = value @timestamps[key] = Time.now end end |
#size ⇒ Integer
Get number of entries in cache
90 91 92 |
# File 'lib/takagi/event_bus/lru_cache.rb', line 90 def size @mutex.synchronize { @cache.size } end |
#stats ⇒ Hash
Get cache statistics
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/takagi/event_bus/lru_cache.rb', line 121 def stats @mutex.synchronize do cleanup_expired { size: @cache.size, max_size: @max_size, ttl: @ttl, utilization: (@cache.size.to_f / @max_size * 100).round(2) } end end |