Class: DecisionAgent::Dmn::EvaluationCache
- Inherits:
-
Object
- Object
- DecisionAgent::Dmn::EvaluationCache
- Defined in:
- lib/decision_agent/dmn/cache.rb
Overview
DMN Evaluation Cache Provides caching for DMN model parsing and evaluation results
Instance Attribute Summary collapse
-
#model_cache ⇒ Object
readonly
Returns the value of attribute model_cache.
-
#result_cache ⇒ Object
readonly
Returns the value of attribute result_cache.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
Instance Method Summary collapse
-
#cache_model(model_id, model) ⇒ Object
Cache a parsed DMN model.
-
#cache_result(decision_id, context_hash, result) ⇒ Object
Cache an evaluation result.
-
#clear ⇒ Object
Clear all caches.
-
#clear_models ⇒ Object
Clear model cache.
-
#clear_results ⇒ Object
Clear result cache.
-
#get_model(model_id) ⇒ Object
Get a cached model.
-
#get_result(decision_id, context_hash) ⇒ Object
Get a cached evaluation result.
-
#initialize(max_model_cache_size: 100, max_result_cache_size: 1000, ttl: 3600) ⇒ EvaluationCache
constructor
A new instance of EvaluationCache.
-
#statistics ⇒ Object
Get cache statistics.
Constructor Details
#initialize(max_model_cache_size: 100, max_result_cache_size: 1000, ttl: 3600) ⇒ EvaluationCache
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/decision_agent/dmn/cache.rb', line 13 def initialize(max_model_cache_size: 100, max_result_cache_size: 1000, ttl: 3600) @model_cache = {} @result_cache = {} @max_model_cache_size = max_model_cache_size @max_result_cache_size = max_result_cache_size @ttl = ttl # Time to live in seconds @mutex = Mutex.new @stats = { model_cache_hits: 0, model_cache_misses: 0, result_cache_hits: 0, result_cache_misses: 0 } end |
Instance Attribute Details
#model_cache ⇒ Object (readonly)
Returns the value of attribute model_cache.
11 12 13 |
# File 'lib/decision_agent/dmn/cache.rb', line 11 def model_cache @model_cache end |
#result_cache ⇒ Object (readonly)
Returns the value of attribute result_cache.
11 12 13 |
# File 'lib/decision_agent/dmn/cache.rb', line 11 def result_cache @result_cache end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
11 12 13 |
# File 'lib/decision_agent/dmn/cache.rb', line 11 def stats @stats end |
Instance Method Details
#cache_model(model_id, model) ⇒ Object
Cache a parsed DMN model
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/decision_agent/dmn/cache.rb', line 29 def cache_model(model_id, model) @mutex.synchronize do # Evict oldest if cache is full evict_oldest_model if @model_cache.size >= @max_model_cache_size @model_cache[model_id] = { model: model, cached_at: Time.now.to_i } end end |
#cache_result(decision_id, context_hash, result) ⇒ Object
Cache an evaluation result
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/decision_agent/dmn/cache.rb', line 58 def cache_result(decision_id, context_hash, result) @mutex.synchronize do # Evict oldest if cache is full evict_oldest_result if @result_cache.size >= @max_result_cache_size cache_key = generate_result_key(decision_id, context_hash) @result_cache[cache_key] = { result: result, cached_at: Time.now.to_i } end end |
#clear ⇒ Object
Clear all caches
89 90 91 92 93 94 95 |
# File 'lib/decision_agent/dmn/cache.rb', line 89 def clear @mutex.synchronize do @model_cache.clear @result_cache.clear @stats.each_key { |k| @stats[k] = 0 } end end |
#clear_models ⇒ Object
Clear model cache
98 99 100 101 102 |
# File 'lib/decision_agent/dmn/cache.rb', line 98 def clear_models @mutex.synchronize do @model_cache.clear end end |
#clear_results ⇒ Object
Clear result cache
105 106 107 108 109 |
# File 'lib/decision_agent/dmn/cache.rb', line 105 def clear_results @mutex.synchronize do @result_cache.clear end end |
#get_model(model_id) ⇒ Object
Get a cached model
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/decision_agent/dmn/cache.rb', line 42 def get_model(model_id) @mutex.synchronize do entry = @model_cache[model_id] if entry && !expired?(entry[:cached_at]) @stats[:model_cache_hits] += 1 entry[:model] else @stats[:model_cache_misses] += 1 @model_cache.delete(model_id) if entry nil end end end |
#get_result(decision_id, context_hash) ⇒ Object
Get a cached evaluation result
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/decision_agent/dmn/cache.rb', line 72 def get_result(decision_id, context_hash) @mutex.synchronize do cache_key = generate_result_key(decision_id, context_hash) entry = @result_cache[cache_key] if entry && !expired?(entry[:cached_at]) @stats[:result_cache_hits] += 1 entry[:result] else @stats[:result_cache_misses] += 1 @result_cache.delete(cache_key) if entry nil end end end |
#statistics ⇒ Object
Get cache statistics
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/decision_agent/dmn/cache.rb', line 112 def statistics @mutex.synchronize do model_hit_rate = calculate_hit_rate(@stats[:model_cache_hits], @stats[:model_cache_misses]) result_hit_rate = calculate_hit_rate(@stats[:result_cache_hits], @stats[:result_cache_misses]) @stats.merge( model_cache_size: @model_cache.size, result_cache_size: @result_cache.size, model_hit_rate: model_hit_rate, result_hit_rate: result_hit_rate ) end end |