Class: Vangrail::ResultCache
- Inherits:
-
Object
- Object
- Vangrail::ResultCache
- 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 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
-
#hits ⇒ Object
readonly
Returns the value of attribute hits.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#misses ⇒ Object
readonly
Returns the value of attribute misses.
Instance Method Summary collapse
- #clear ⇒ Object
-
#fetch(side, name, key) ⇒ Object
Yields on a miss and stores what the block returns.
-
#initialize(limit: DEFAULT_LIMIT) ⇒ ResultCache
constructor
A new instance of ResultCache.
- #size ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(limit: DEFAULT_LIMIT) ⇒ ResultCache
Returns a new instance of ResultCache.
19 20 21 22 23 24 25 |
# File 'lib/vangrail/result_cache.rb', line 19 def initialize(limit: DEFAULT_LIMIT) @limit = limit @store = {} @hits = 0 @misses = 0 @mutex = Mutex.new end |
Instance Attribute Details
#hits ⇒ Object (readonly)
Returns the value of attribute hits.
17 18 19 |
# File 'lib/vangrail/result_cache.rb', line 17 def hits @hits end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
17 18 19 |
# File 'lib/vangrail/result_cache.rb', line 17 def limit @limit end |
#misses ⇒ Object (readonly)
Returns the value of attribute misses.
17 18 19 |
# File 'lib/vangrail/result_cache.rb', line 17 def misses @misses end |
Instance Method Details
#clear ⇒ Object
51 52 53 |
# File 'lib/vangrail/result_cache.rb', line 51 def clear @mutex.synchronize { @store.clear } end |
#fetch(side, name, key) ⇒ Object
Yields on a miss and stores what the block returns. Hits move to the
newest slot so the oldest unused key is the one shift drops.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/vangrail/result_cache.rb', line 29 def fetch(side, name, key) store_key = [side, name, key] hit = @mutex.synchronize do next unless @store.key?(store_key) @hits += 1 value = @store.delete(store_key) @store[store_key] = value value end return hit if hit @mutex.synchronize { @misses += 1 } result = yield store(store_key, result) result end |
#size ⇒ Object
47 48 49 |
# File 'lib/vangrail/result_cache.rb', line 47 def size @mutex.synchronize { @store.size } end |
#to_h ⇒ Object
55 56 57 |
# File 'lib/vangrail/result_cache.rb', line 55 def to_h { 'size' => size, 'limit' => limit, 'hits' => hits, 'misses' => misses } end |