Class: McpAuthorization::Cache::MemoryStore

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/mcp_authorization/cache/memory_store.rb

Overview

Process-local in-memory store with a bounded entry count and per-entry TTL. Each worker process warms its own copy — fine for the tools/list payload, which is identical across workers for a given decision vector. For cross-process/host sharing, use RedisStore.

Constant Summary collapse

DEFAULT_MAX_ENTRIES =
512

Instance Method Summary collapse

Constructor Details

#initialize(max_entries: DEFAULT_MAX_ENTRIES) ⇒ MemoryStore

: (?max_entries: Integer) -> void



15
16
17
18
19
# File 'lib/mcp_authorization/cache/memory_store.rb', line 15

def initialize(max_entries: DEFAULT_MAX_ENTRIES)
  super()
  @max_entries = max_entries
  @entries = {} #: Hash[String, [untyped, Float?]]  # key => [value, expires_at]
end

Instance Method Details

#clearObject

: () -> void



50
51
52
# File 'lib/mcp_authorization/cache/memory_store.rb', line 50

def clear
  synchronize { @entries.clear }
end

#get(key) ⇒ Object

: (String) -> untyped



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mcp_authorization/cache/memory_store.rb', line 22

def get(key)
  synchronize do
    entry = @entries[key]
    return nil unless entry

    value, expires_at = entry
    if expires_at && monotonic > expires_at
      @entries.delete(key)
      return nil
    end

    # Mark as most-recently-used.
    @entries.delete(key)
    @entries[key] = entry
    value
  end
end

#set(key, value, ttl: nil) ⇒ Object

: (String, untyped, ?ttl: Integer?) -> void



41
42
43
44
45
46
47
# File 'lib/mcp_authorization/cache/memory_store.rb', line 41

def set(key, value, ttl: nil)
  synchronize do
    @entries.delete(key)
    @entries[key] = [value, ttl ? monotonic + ttl : nil]
    @entries.shift while @entries.size > @max_entries # evict oldest (insertion order)
  end
end