Class: DecisionAgent::Dmn::FeelExpressionCache

Inherits:
Object
  • Object
show all
Defined in:
lib/decision_agent/dmn/cache.rb

Overview

FEEL Expression Cache Caches compiled/parsed FEEL expressions for reuse

Instance Method Summary collapse

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

#clearObject

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

#statisticsObject

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