Class: EndPointBlank::Commands::AuthenticationCache
- Inherits:
-
Object
- Object
- EndPointBlank::Commands::AuthenticationCache
- 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
-
#clear ⇒ Hash
Clear all credentials from the cache.
-
#exists?(key) ⇒ Boolean
Check if credentials exist for a given key.
-
#initialize ⇒ AuthenticationCache
constructor
A new instance of AuthenticationCache.
-
#keys ⇒ Array
Get all keys in the cache.
-
#remove(key) ⇒ Object?
Remove credentials from the cache.
-
#retrieve(key) ⇒ Object?
Retrieve credentials from the cache.
-
#size ⇒ Integer
Get the number of credentials stored.
-
#store(key, credentials) ⇒ Object
Store credentials in the cache.
Constructor Details
#initialize ⇒ AuthenticationCache
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
#clear ⇒ Hash
Clear all credentials from the cache
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
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 |
#keys ⇒ Array
Get all keys in the cache
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
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
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 |
#size ⇒ Integer
Get the number of credentials stored
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
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 |