Module: Flightdeck::Cache

Defined in:
app/models/flightdeck/cache.rb

Overview

Thin wrapper over Rails.cache for the dashboard's short-lived aggregates.

Two things it guarantees:

* it degrades to simply calling the block, so a host running the null
store (or no cache at all) still gets a working dashboard;
* a mutation can ask for fresh numbers via .bypass, so the response to a
retry never shows the counts from before the retry.

Constant Summary collapse

NAMESPACE =
"flightdeck"

Class Method Summary collapse

Class Method Details

.bypassObject

Inside this block every fetch recomputes and rewrites, so a page rendered straight after a mutation reflects it.



27
28
29
30
31
32
33
# File 'app/models/flightdeck/cache.rb', line 27

def bypass
  previous = Thread.current[:flightdeck_cache_bypass]
  Thread.current[:flightdeck_cache_bypass] = true
  yield
ensure
  Thread.current[:flightdeck_cache_bypass] = previous
end

.bypass?Boolean

Returns:

  • (Boolean)


35
# File 'app/models/flightdeck/cache.rb', line 35

def bypass? = Thread.current[:flightdeck_cache_bypass].present?

.fetch(*key_parts, expires_in:, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'app/models/flightdeck/cache.rb', line 15

def fetch(*key_parts, expires_in:, &block)
  store = Rails.cache
  return block.call if store.nil? || expires_in.to_f <= 0

  store.fetch(key_for(key_parts), expires_in: expires_in, force: bypass?, &block)
rescue StandardError
  # A cache backend that is down must never take the dashboard with it.
  block.call
end

.key_for(parts) ⇒ Object



37
38
39
# File 'app/models/flightdeck/cache.rb', line 37

def key_for(parts)
  [ NAMESPACE, *Array(parts).flatten.map { |part| normalize(part) } ].join(":")
end