Class: Fractor::ResultCache
- Inherits:
-
Object
- Object
- Fractor::ResultCache
- Defined in:
- lib/fractor/result_cache.rb
Overview
Caches work results to avoid redundant processing of identical work items. Useful for expensive, deterministic operations.
Instance Attribute Summary collapse
-
#hits ⇒ Object
readonly
Returns the value of attribute hits.
-
#misses ⇒ Object
readonly
Returns the value of attribute misses.
Instance Method Summary collapse
-
#cleanup_expired ⇒ Integer
Remove expired entries from the cache.
-
#clear ⇒ void
Clear all cached results.
-
#get(work) { ... } ⇒ WorkResult, Object
Get a cached result or compute and cache it.
-
#has?(work) ⇒ Boolean
Check if a work item has a cached result.
-
#initialize(ttl: nil, max_size: nil, max_memory: nil) ⇒ ResultCache
constructor
Initialize a new result cache.
-
#invalidate(work) ⇒ Boolean
Invalidate a cached result.
-
#set(work, result) ⇒ void
Store a result in the cache.
-
#size ⇒ Integer
Get the current cache size.
-
#stats ⇒ Hash
Get cache statistics.
Constructor Details
#initialize(ttl: nil, max_size: nil, max_memory: nil) ⇒ ResultCache
Initialize a new result cache.
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/fractor/result_cache.rb', line 24 def initialize(ttl: nil, max_size: nil, max_memory: nil) @cache = {} @timestamps = {} @access_times = {} @ttl = ttl @max_size = max_size @max_memory = max_memory @current_memory = 0 @mutex = Mutex.new @hits = 0 @misses = 0 end |
Instance Attribute Details
#hits ⇒ Object (readonly)
Returns the value of attribute hits.
17 18 19 |
# File 'lib/fractor/result_cache.rb', line 17 def hits @hits end |
#misses ⇒ Object (readonly)
Returns the value of attribute misses.
17 18 19 |
# File 'lib/fractor/result_cache.rb', line 17 def misses @misses end |
Instance Method Details
#cleanup_expired ⇒ Integer
Remove expired entries from the cache.
147 148 149 150 151 152 153 |
# File 'lib/fractor/result_cache.rb', line 147 def cleanup_expired @mutex.synchronize do expired_keys = @cache.keys.select { |key| expired?(key) } expired_keys.each { |key| remove_entry(key) } expired_keys.size end end |
#clear ⇒ void
This method returns an undefined value.
Clear all cached results.
117 118 119 120 121 122 123 124 |
# File 'lib/fractor/result_cache.rb', line 117 def clear @mutex.synchronize do @cache.clear @timestamps.clear @access_times.clear @current_memory = 0 end end |
#get(work) { ... } ⇒ WorkResult, Object
Get a cached result or compute and cache it.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/fractor/result_cache.rb', line 49 def get(work) key = generate_key(work) @mutex.synchronize do # Check if we have a valid cached result if @cache.key?(key) && !expired?(key) @hits += 1 @access_times[key] = Time.now return @cache[key] end @misses += 1 # Compute the result result = yield # Cache the result cache_entry(key, result) result end end |
#has?(work) ⇒ Boolean
Check if a work item has a cached result.
76 77 78 79 80 81 82 |
# File 'lib/fractor/result_cache.rb', line 76 def has?(work) key = generate_key(work) @mutex.synchronize do @cache.key?(key) && !expired?(key) end end |
#invalidate(work) ⇒ Boolean
Invalidate a cached result.
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/fractor/result_cache.rb', line 101 def invalidate(work) key = generate_key(work) @mutex.synchronize do if @cache.key?(key) remove_entry(key) true else false end end end |
#set(work, result) ⇒ void
This method returns an undefined value.
Store a result in the cache.
89 90 91 92 93 94 95 |
# File 'lib/fractor/result_cache.rb', line 89 def set(work, result) key = generate_key(work) @mutex.synchronize do cache_entry(key, result) end end |
#size ⇒ Integer
Get the current cache size.
40 41 42 |
# File 'lib/fractor/result_cache.rb', line 40 def size @mutex.synchronize { @cache.size } end |
#stats ⇒ Hash
Get cache statistics.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/fractor/result_cache.rb', line 129 def stats @mutex.synchronize do total = @hits + @misses hit_rate = total.positive? ? (@hits.to_f / total * 100).round(2) : 0 { size: @cache.size, hits: @hits, misses: @misses, hit_rate: hit_rate, current_memory: @current_memory, } end end |