Class: DecisionAgent::Dmn::FeelExpressionCache
- Inherits:
-
Object
- Object
- DecisionAgent::Dmn::FeelExpressionCache
- Defined in:
- lib/decision_agent/dmn/cache.rb
Overview
FEEL Expression Cache Caches compiled/parsed FEEL expressions for reuse
Instance Method Summary collapse
-
#cache_expression(expression_string, parsed_expression) ⇒ Object
Cache a parsed FEEL expression.
-
#clear ⇒ Object
Clear cache.
-
#get_expression(expression_string) ⇒ Object
Get a cached expression.
-
#initialize(max_size: 500) ⇒ FeelExpressionCache
constructor
A new instance of FeelExpressionCache.
-
#statistics ⇒ Object
Get statistics.
Constructor Details
#initialize(max_size: 500) ⇒ FeelExpressionCache
Returns a new instance of FeelExpressionCache.
227 228 229 230 231 232 |
# File 'lib/decision_agent/dmn/cache.rb', line 227 def initialize(max_size: 500) @cache = {} @max_size = max_size @mutex = Mutex.new @stats = { hits: 0, misses: 0 } end |
Instance Method Details
#cache_expression(expression_string, parsed_expression) ⇒ Object
Cache a parsed FEEL expression
235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/decision_agent/dmn/cache.rb', line 235 def cache_expression(expression_string, parsed_expression) @mutex.synchronize do evict_oldest if @cache.size >= @max_size @cache[expression_string] = { expression: parsed_expression, accessed_at: Time.now.to_i, access_count: 0 } end end |
#clear ⇒ Object
Clear cache
265 266 267 268 269 270 271 |
# File 'lib/decision_agent/dmn/cache.rb', line 265 def clear @mutex.synchronize do @cache.clear @stats[:hits] = 0 @stats[:misses] = 0 end end |
#get_expression(expression_string) ⇒ Object
Get a cached expression
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/decision_agent/dmn/cache.rb', line 248 def get_expression(expression_string) @mutex.synchronize do entry = @cache[expression_string] if entry @stats[:hits] += 1 entry[:accessed_at] = Time.now.to_i entry[:access_count] += 1 entry[:expression] else @stats[:misses] += 1 nil end end end |
#statistics ⇒ Object
Get statistics
274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/decision_agent/dmn/cache.rb', line 274 def statistics @mutex.synchronize do hit_rate = @stats[:hits] + @stats[:misses] hit_rate = hit_rate.positive? ? (@stats[:hits].to_f / hit_rate * 100).round(2) : 0 { size: @cache.size, hits: @stats[:hits], misses: @stats[:misses], hit_rate: hit_rate, most_accessed: most_accessed_expressions } end end |