Class: SmartPrompt::LRUCache
- Inherits:
-
Object
- Object
- SmartPrompt::LRUCache
- Defined in:
- lib/smart_prompt/lru_cache.rb
Overview
LRUCache implements a Least Recently Used cache with size limit enforcement Thread-safe implementation for managing session cache
Instance Attribute Summary collapse
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
Instance Method Summary collapse
-
#clear ⇒ Object
Clear all entries from the cache.
-
#delete(key) ⇒ Object
Delete a key from the cache.
-
#each(&block) ⇒ Object
Iterate over cache entries.
-
#empty? ⇒ Boolean
Check if cache is empty.
-
#get(key) ⇒ Object
Get a value from the cache Updates access time for LRU tracking.
-
#initialize(max_size = nil) ⇒ LRUCache
constructor
A new instance of LRUCache.
-
#key?(key) ⇒ Boolean
Check if a key exists in the cache.
-
#keys ⇒ Object
Get all keys in the cache.
-
#lru_key ⇒ Object
Get the least recently used key.
-
#put(key, value) ⇒ Object
Put a value into the cache Enforces size limit by evicting least recently used entry if needed.
-
#size ⇒ Object
Get the current size of the cache.
-
#values ⇒ Object
Get all values in the cache.
Constructor Details
#initialize(max_size = nil) ⇒ LRUCache
Returns a new instance of LRUCache.
9 10 11 12 13 14 |
# File 'lib/smart_prompt/lru_cache.rb', line 9 def initialize(max_size = nil) @max_size = max_size @cache = {} @access_times = {} @mutex = Mutex.new end |
Instance Attribute Details
#max_size ⇒ Object (readonly)
Returns the value of attribute max_size.
7 8 9 |
# File 'lib/smart_prompt/lru_cache.rb', line 7 def max_size @max_size end |
Instance Method Details
#clear ⇒ Object
Clear all entries from the cache
90 91 92 93 94 95 |
# File 'lib/smart_prompt/lru_cache.rb', line 90 def clear @mutex.synchronize do @cache.clear @access_times.clear end end |
#delete(key) ⇒ Object
Delete a key from the cache
60 61 62 63 64 65 |
# File 'lib/smart_prompt/lru_cache.rb', line 60 def delete(key) @mutex.synchronize do @access_times.delete(key) @cache.delete(key) end end |
#each(&block) ⇒ Object
Iterate over cache entries
112 113 114 115 116 |
# File 'lib/smart_prompt/lru_cache.rb', line 112 def each(&block) @mutex.synchronize do @cache.each(&block) end end |
#empty? ⇒ Boolean
Check if cache is empty
105 106 107 108 109 |
# File 'lib/smart_prompt/lru_cache.rb', line 105 def empty? @mutex.synchronize do @cache.empty? end end |
#get(key) ⇒ Object
Get a value from the cache Updates access time for LRU tracking
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/smart_prompt/lru_cache.rb', line 18 def get(key) @mutex.synchronize do if @cache.key?(key) @access_times[key] = Time.now @cache[key] else nil end end end |
#key?(key) ⇒ Boolean
Check if a key exists in the cache
53 54 55 56 57 |
# File 'lib/smart_prompt/lru_cache.rb', line 53 def key?(key) @mutex.synchronize do @cache.key?(key) end end |
#keys ⇒ Object
Get all keys in the cache
68 69 70 71 72 |
# File 'lib/smart_prompt/lru_cache.rb', line 68 def keys @mutex.synchronize do @cache.keys end end |
#lru_key ⇒ Object
Get the least recently used key
82 83 84 85 86 87 |
# File 'lib/smart_prompt/lru_cache.rb', line 82 def lru_key @mutex.synchronize do return nil if @access_times.empty? @access_times.min_by { |_, time| time }&.first end end |
#put(key, value) ⇒ Object
Put a value into the cache Enforces size limit by evicting least recently used entry if needed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/smart_prompt/lru_cache.rb', line 31 def put(key, value) @mutex.synchronize do # If key already exists, just update it if @cache.key?(key) @cache[key] = value @access_times[key] = Time.now return value end # Enforce size limit before adding new entry if @max_size && @cache.size >= @max_size evict_lru end # Add new entry @cache[key] = value @access_times[key] = Time.now value end end |
#size ⇒ Object
Get the current size of the cache
75 76 77 78 79 |
# File 'lib/smart_prompt/lru_cache.rb', line 75 def size @mutex.synchronize do @cache.size end end |
#values ⇒ Object
Get all values in the cache
98 99 100 101 102 |
# File 'lib/smart_prompt/lru_cache.rb', line 98 def values @mutex.synchronize do @cache.values end end |