Class: Karafka::Web::Ui::Lib::Cache
- Inherits:
-
Object
- Object
- Karafka::Web::Ui::Lib::Cache
- Defined in:
- lib/karafka/web/ui/lib/cache.rb
Overview
All cache operations are mutex-synchronized for thread safety.
We do not have granular level caching because our Web UI cache is fairly simple and we do not want to overcomplicate things.
Thread-safe in-memory cache with metadata tracking.
This cache supports storing computed values, tracking the last update time, and computing a hash of the contents for change detection. It’s designed for ephemeral, per-instance caching in Karafka Web controllers or libs.
The cache ensures safe concurrent access via a mutex and provides utilities for cache invalidation based on external session state (timestamp + hash).
Instance Method Summary collapse
-
#clear ⇒ void
Clears the cache and resets metadata (timestamp and hash).
-
#clear_if_needed(session_hash, session_timestamp) ⇒ Boolean
Clears the cache if the provided session hash and timestamp differ.
-
#exist? ⇒ Boolean
Checks whether any values have been cached yet.
-
#fetch(key) { ... } ⇒ Object
Fetches or computes and stores a value under the given key.
-
#hash ⇒ String?
Returns the hash representing the current cached data state.
-
#initialize(ttl_ms) ⇒ Cache
constructor
Initializes an empty cache instance.
-
#timestamp ⇒ Integer?
Returns the last update timestamp of the cache.
Constructor Details
#initialize(ttl_ms) ⇒ Cache
Initializes an empty cache instance
24 25 26 27 28 29 30 |
# File 'lib/karafka/web/ui/lib/cache.rb', line 24 def initialize(ttl_ms) @ttl_ms = ttl_ms @values = {} @timestamp = nil @hash = nil @mutex = Mutex.new end |
Instance Method Details
#clear ⇒ void
This method returns an undefined value.
Clears the cache and resets metadata (timestamp and hash).
If the mutex is already owned by the current thread, clears immediately. Otherwise, synchronizes first.
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/karafka/web/ui/lib/cache.rb', line 60 def clear cleaning = lambda do @values.clear @timestamp = nil @hash = nil end return cleaning.call if @mutex.owned? @mutex.synchronize do cleaning.call end end |
#clear_if_needed(session_hash, session_timestamp) ⇒ Boolean
Clears the cache if the provided session hash and timestamp differ
This is used to invalidate the cache if the external session data indicates a newer or inconsistent state.
103 104 105 106 107 108 109 |
# File 'lib/karafka/web/ui/lib/cache.rb', line 103 def clear_if_needed(session_hash, ) @mutex.synchronize do return unless should_refresh?(session_hash, ) clear end end |
#exist? ⇒ Boolean
Checks whether any values have been cached yet
77 78 79 |
# File 'lib/karafka/web/ui/lib/cache.rb', line 77 def exist? !.nil? end |
#fetch(key) { ... } ⇒ Object
Fetches or computes and stores a value under the given key.
If the key already exists, returns the cached value. Otherwise, computes it via the provided block, stores it, and updates metadata (timestamp + hash).
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/karafka/web/ui/lib/cache.rb', line 41 def fetch(key) @mutex.synchronize do return @values[key] if @values.key?(key) @values[key] = yield @hash = Digest::SHA256.hexdigest( @values.sort.to_h.to_json ) @timestamp = Time.now.to_f @values[key] end end |
#hash ⇒ String?
Returns the hash representing the current cached data state
91 92 93 |
# File 'lib/karafka/web/ui/lib/cache.rb', line 91 def hash @mutex.synchronize { @hash } end |
#timestamp ⇒ Integer?
Returns the last update timestamp of the cache
84 85 86 |
# File 'lib/karafka/web/ui/lib/cache.rb', line 84 def @mutex.synchronize { @timestamp } end |