Class: Vangrail::ResultCache

Inherits:
Object
  • Object
show all
Defined in:
lib/vangrail/result_cache.rb

Overview

Bounded in-process memo for rail results.

A reader who rephrases, retries, or clicks a suggested follow-up sends text a rail has already judged, and each repeat costs another round trip. Keyed on side, rail name, and a digest of whatever that rail says its decision depends on, so a changed model or a changed policy never reads a stale decision.

Only decided results are stored. A result with certain? == false records a rail that failed or did not run, and caching that would turn one unlucky moment into a session-long hole.

Constant Summary collapse

DEFAULT_LIMIT =
256

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(limit: DEFAULT_LIMIT) ⇒ ResultCache

Returns a new instance of ResultCache.



21
22
23
24
25
26
27
# File 'lib/vangrail/result_cache.rb', line 21

def initialize(limit: DEFAULT_LIMIT)
  @limit = limit
  @store = {}
  @hits = 0
  @misses = 0
  @mutex = Mutex.new
end

Instance Attribute Details

#hitsObject (readonly)

Returns the value of attribute hits.



19
20
21
# File 'lib/vangrail/result_cache.rb', line 19

def hits
  @hits
end

#limitObject (readonly)

Returns the value of attribute limit.



19
20
21
# File 'lib/vangrail/result_cache.rb', line 19

def limit
  @limit
end

#missesObject (readonly)

Returns the value of attribute misses.



19
20
21
# File 'lib/vangrail/result_cache.rb', line 19

def misses
  @misses
end

Instance Method Details

#clearObject



48
49
50
# File 'lib/vangrail/result_cache.rb', line 48

def clear
  @mutex.synchronize { @store.clear }
end

#fetch(rail, model, text) ⇒ Object

Yields on a miss and stores what the block returns.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vangrail/result_cache.rb', line 30

def fetch(rail, model, text)
  key = key_for(rail, model, text)
  hit = @mutex.synchronize { @store[key] }
  if hit
    @mutex.synchronize { @hits += 1 }
    return hit
  end

  @mutex.synchronize { @misses += 1 }
  result = yield
  store(key, result)
  result
end

#sizeObject



44
45
46
# File 'lib/vangrail/result_cache.rb', line 44

def size
  @mutex.synchronize { @store.size }
end

#to_hObject



52
53
54
# File 'lib/vangrail/result_cache.rb', line 52

def to_h
  { 'size' => size, 'limit' => limit, 'hits' => hits, 'misses' => misses }
end