Module: Subflag::Rails::RequestCache
- Defined in:
- lib/subflag/rails/request_cache.rb
Overview
Per-request cache for flag evaluations
Caches flag values for the duration of a single request to avoid multiple API calls for the same flag.
Defined Under Namespace
Classes: Middleware
Class Method Summary collapse
-
.clear ⇒ Object
End the cache scope.
-
.current_cache ⇒ Object
Get current cache.
-
.enabled? ⇒ Boolean
Check if request caching is active.
-
.fetch(cache_key) { ... } ⇒ Object
Get a cached value or yield to fetch it.
-
.start ⇒ Object
Start a new cache scope.
-
.stats ⇒ Object
Get cache stats for debugging.
Class Method Details
.clear ⇒ Object
End the cache scope
45 46 47 |
# File 'lib/subflag/rails/request_cache.rb', line 45 def clear Thread.current[:subflag_request_cache] = nil end |
.current_cache ⇒ Object
Get current cache
50 51 52 |
# File 'lib/subflag/rails/request_cache.rb', line 50 def current_cache Thread.current[:subflag_request_cache] ||= {} end |
.enabled? ⇒ Boolean
Check if request caching is active
35 36 37 |
# File 'lib/subflag/rails/request_cache.rb', line 35 def enabled? Thread.current[:subflag_request_cache].is_a?(Hash) end |
.fetch(cache_key) { ... } ⇒ Object
Get a cached value or yield to fetch it
25 26 27 28 29 30 31 32 |
# File 'lib/subflag/rails/request_cache.rb', line 25 def fetch(cache_key, &block) return yield unless enabled? cache = current_cache return cache[cache_key] if cache.key?(cache_key) cache[cache_key] = yield end |
.start ⇒ Object
Start a new cache scope
40 41 42 |
# File 'lib/subflag/rails/request_cache.rb', line 40 def start Thread.current[:subflag_request_cache] = {} end |
.stats ⇒ Object
Get cache stats for debugging
55 56 57 58 |
# File 'lib/subflag/rails/request_cache.rb', line 55 def stats cache = current_cache { size: cache.size, keys: cache.keys } end |