Class: Karafka::Web::Ui::Lib::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/web/ui/lib/cache.rb

Overview

Note:

All cache operations are mutex-synchronized for thread safety.

Note:

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

Constructor Details

#initialize(ttl_ms) ⇒ Cache

Initializes an empty cache instance

Parameters:

  • ttl_ms (Integer)

    time to live of the whole cache. After this time cache will be cleaned whether or not it is expired.



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

#clearvoid

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.

Parameters:

  • session_hash (String, nil)

    hash from the session or remote side

  • session_timestamp (Integer, nil)

    timestamp from the session

Returns:

  • (Boolean)

    true if the cache was cleared, false otherwise



103
104
105
106
107
108
109
# File 'lib/karafka/web/ui/lib/cache.rb', line 103

def clear_if_needed(session_hash, session_timestamp)
  @mutex.synchronize do
    return unless should_refresh?(session_hash, session_timestamp)

    clear
  end
end

#exist?Boolean

Checks whether any values have been cached yet

Returns:

  • (Boolean)

    true if the cache has been written to



77
78
79
# File 'lib/karafka/web/ui/lib/cache.rb', line 77

def exist?
  !timestamp.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).

Parameters:

  • key (Object)

    key to retrieve

Yields:

  • block to compute the value if key is not present

Returns:

  • (Object)

    cached or computed value



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

#hashString?

Returns the hash representing the current cached data state

Returns:

  • (String, nil)

    SHA256 hex digest or nil if never set



91
92
93
# File 'lib/karafka/web/ui/lib/cache.rb', line 91

def hash
  @mutex.synchronize { @hash }
end

#timestampInteger?

Returns the last update timestamp of the cache

Returns:

  • (Integer, nil)

    Unix timestamp or nil if never set



84
85
86
# File 'lib/karafka/web/ui/lib/cache.rb', line 84

def timestamp
  @mutex.synchronize { @timestamp }
end