Class: Identizer::GrantStore

Inherits:
Object
  • Object
show all
Defined in:
lib/identizer/grant_store.rb

Overview

A small thread-safe, TTL’d key -> value store for the short-lived grants the provider issues (authorization codes, access tokens, refresh tokens). Entries expire and are pruned lazily on access, plus an opportunistic sweep once the store grows, so even never-redeemed grants don’t accumulate without bound. The advertised lifetimes are enforced. Uses a monotonic clock (immune to wall-clock changes).

Constant Summary collapse

SWEEP_THRESHOLD =
1000

Instance Method Summary collapse

Constructor Details

#initializeGrantStore

Returns a new instance of GrantStore.



13
14
15
16
# File 'lib/identizer/grant_store.rb', line 13

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

Instance Method Details

#get(key) ⇒ Object

Read without consuming; nil if missing or expired.



27
28
29
# File 'lib/identizer/grant_store.rb', line 27

def get(key)
  @mutex.synchronize { fetch(key) }
end

#put(key, value, ttl:) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/identizer/grant_store.rb', line 18

def put(key, value, ttl:)
  @mutex.synchronize do
    sweep if @entries.size >= SWEEP_THRESHOLD
    @entries[key] = [value, monotonic + ttl]
  end
  value
end

#sizeObject



40
41
42
# File 'lib/identizer/grant_store.rb', line 40

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

#take(key) ⇒ Object

Read and remove (single-use); nil if missing or expired.



32
33
34
35
36
37
38
# File 'lib/identizer/grant_store.rb', line 32

def take(key)
  @mutex.synchronize do
    value = fetch(key)
    @entries.delete(key)
    value
  end
end