Class: Restless::EnrichCache

Inherits:
Object
  • Object
show all
Defined in:
lib/restless/caches.rb

Overview

CACHE-001..007. Enriched owner metadata, keyed by owner id (or the masked end-user key when there is no id).

The point is that enrich runs once per key per TTL window rather than once per request, AND that the enriched VALUE is stored -- not merely a freshness flag. Every upload has to carry owner metadata, including the ones that skipped the callback, because the ingest cannot backfill: without it every request after the first lands in the dashboard as unauthenticated.

Constant Summary collapse

DEFAULT_TTL_MS =

CACHE-004: 1 hour

3_600_000

Instance Method Summary collapse

Constructor Details

#initialize(ttl_ms = DEFAULT_TTL_MS) ⇒ EnrichCache

Returns a new instance of EnrichCache.



21
22
23
24
25
# File 'lib/restless/caches.rb', line 21

def initialize(ttl_ms = DEFAULT_TTL_MS)
  @ttl_ms = ttl_ms
  @entries = {}
  @mutex = Mutex.new
end

Instance Method Details

#clearObject



50
51
52
# File 'lib/restless/caches.rb', line 50

def clear
  @mutex.synchronize { @entries.clear }
end

#get(key) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/restless/caches.rb', line 27

def get(key)
  @mutex.synchronize do
    entry = @entries[key]
    next nil if entry.nil?

    if now_ms - entry[:ts] > @ttl_ms # CACHE-015
      @entries.delete(key)
      next nil
    end
    entry[:value]
  end
end

#invalidate(key) ⇒ Object

CACHE-006. Server-driven, via needsEnrichment on an upload response.



46
47
48
# File 'lib/restless/caches.rb', line 46

def invalidate(key)
  @mutex.synchronize { @entries.delete(key) }
end

#set(key, value) ⇒ Object



40
41
42
43
# File 'lib/restless/caches.rb', line 40

def set(key, value)
  @mutex.synchronize { @entries[key] = { value: value, ts: now_ms } }
  value
end

#sizeObject



54
55
56
# File 'lib/restless/caches.rb', line 54

def size
  @mutex.synchronize { @entries.size }
end