Class: EndPointBlank::Commands::AuthenticationCache

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/end_point_blank/commands/authentication_cache.rb

Overview

Thread-safe singleton cache for storing authentication credentials. Capped at MAX_SIZE entries. When full, expired entries are evicted first; if still at capacity the entry expiring soonest is removed.

Constant Summary collapse

MAX_SIZE =
1000

Instance Method Summary collapse

Constructor Details

#initializeAuthenticationCache

Returns a new instance of AuthenticationCache.



15
16
17
18
# File 'lib/end_point_blank/commands/authentication_cache.rb', line 15

def initialize
  @cache = {}
  @mutex = Mutex.new
end

Instance Method Details

#clearHash

Clear all credentials from the cache

Returns:

  • (Hash)

    Empty hash



72
73
74
75
76
# File 'lib/end_point_blank/commands/authentication_cache.rb', line 72

def clear
  @mutex.synchronize do
    @cache.clear
  end
end

#exists?(key) ⇒ Boolean

Check if credentials exist for a given key

Parameters:

  • key (String, Symbol)

    The identifier to check

Returns:

  • (Boolean)

    True if credentials exist, false otherwise



55
56
57
58
59
# File 'lib/end_point_blank/commands/authentication_cache.rb', line 55

def exists?(key)
  @mutex.synchronize do
    @cache.key?(key) && @cache[key][:expired_at] > Time.now
  end
end

#keysArray

Get all keys in the cache

Returns:

  • (Array)

    Array of all keys



80
81
82
83
84
# File 'lib/end_point_blank/commands/authentication_cache.rb', line 80

def keys
  @mutex.synchronize do
    @cache.keys.dup
  end
end

#remove(key) ⇒ Object?

Remove credentials from the cache

Parameters:

  • key (String, Symbol)

    The identifier for the credentials to remove

Returns:

  • (Object, nil)

    The removed credentials or nil if not found



64
65
66
67
68
# File 'lib/end_point_blank/commands/authentication_cache.rb', line 64

def remove(key)
  @mutex.synchronize do
    @cache.delete(key)
  end
end

#retrieve(key) ⇒ Object?

Retrieve credentials from the cache

Parameters:

  • key (String, Symbol)

    The identifier for the credentials

Returns:

  • (Object, nil)

    The stored credentials or nil if not found



42
43
44
45
46
47
48
49
50
# File 'lib/end_point_blank/commands/authentication_cache.rb', line 42

def retrieve(key)
  @mutex.synchronize do
    if @cache.key?(key)
      @cache[key][:credentials] if @cache[key][:expired_at] > Time.now
    else
      nil
    end
  end
end

#sizeInteger

Get the number of credentials stored

Returns:

  • (Integer)

    The size of the cache



88
89
90
91
92
# File 'lib/end_point_blank/commands/authentication_cache.rb', line 88

def size
  @mutex.synchronize do
    @cache.size
  end
end

#store(key, credentials) ⇒ Object

Store credentials in the cache

Parameters:

  • key (String, Symbol)

    The identifier for the credentials

  • credentials (Object)

    The credentials to store

Returns:

  • (Object)

    The stored credentials



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/end_point_blank/commands/authentication_cache.rb', line 24

def store(key, credentials)
  return unless credentials
  @mutex.synchronize do
    now = Time.now
    # Evict expired entries first
    @cache.delete_if { |_, v| v[:expired_at] <= now }
    # Evict the entry expiring soonest if still at capacity
    if @cache.size >= MAX_SIZE
      oldest_key = @cache.min_by { |_, v| v[:expired_at] }.first
      @cache.delete(oldest_key)
    end
    @cache[key] = { expired_at: now + ::EndPointBlank::Configuration.instance.cache_ttl, credentials: credentials }
  end
end