Class: RackJwtVerifier::InProcessCache
- Inherits:
-
Object
- Object
- RackJwtVerifier::InProcessCache
- Defined in:
- lib/rack_jwt_verifier/in_process_cache.rb
Overview
A simple, thread-safe, in-memory cache implementation designed to mimic a real cache store (like Rails.cache or Redis) but without external dependencies. This is the default cache store used if the user doesn't configure one.
Constant Summary collapse
- DEFAULT_EXPIRY =
The cache lifespan in seconds (5 minutes)
300- MONOTONIC_CLOCK =
Expiry is measured on the monotonic clock, so a wall-clock jump (NTP correction, DST) cannot extend or cut short an entry's life.
-> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
- SWEEP_THRESHOLD =
Expired entries are swept whenever the store grows past this many entries, and the threshold then doubles from whatever survived. Amortised O(1) per write, so a jti-per-request workload cannot grow the store without bound while a single-key workload never pays for a sweep.
1024
Instance Method Summary collapse
-
#clear ⇒ Object
Removes every entry.
-
#delete(key) ⇒ Object?
Deletes an entry from the cache.
-
#initialize(clock: MONOTONIC_CLOCK) ⇒ InProcessCache
constructor
A new instance of InProcessCache.
-
#read(key) ⇒ Object?
Reads the value for a given key.
-
#size ⇒ Object
Number of entries held, expired ones included until the next sweep.
-
#write(key, value, options = {}) ⇒ Object, false
Writes a value to the cache with an optional expiration time.
Constructor Details
#initialize(clock: MONOTONIC_CLOCK) ⇒ InProcessCache
Returns a new instance of InProcessCache.
22 23 24 25 26 27 |
# File 'lib/rack_jwt_verifier/in_process_cache.rb', line 22 def initialize(clock: MONOTONIC_CLOCK) @store = {} @lock = Mutex.new # Ensure thread safety for multi-threaded environments @clock = clock @sweep_at = SWEEP_THRESHOLD end |
Instance Method Details
#clear ⇒ Object
Removes every entry.
65 66 67 68 69 70 71 |
# File 'lib/rack_jwt_verifier/in_process_cache.rb', line 65 def clear @lock.synchronize do @store.clear @sweep_at = SWEEP_THRESHOLD end nil end |
#delete(key) ⇒ Object?
Deletes an entry from the cache.
57 58 59 60 61 62 |
# File 'lib/rack_jwt_verifier/in_process_cache.rb', line 57 def delete(key) @lock.synchronize do entry = @store.delete(key) entry&.first end end |
#read(key) ⇒ Object?
Reads the value for a given key. Automatically checks for expiry.
32 33 34 |
# File 'lib/rack_jwt_verifier/in_process_cache.rb', line 32 def read(key) @lock.synchronize { live_value(key) } end |
#size ⇒ Object
Number of entries held, expired ones included until the next sweep.
74 75 76 |
# File 'lib/rack_jwt_verifier/in_process_cache.rb', line 74 def size @lock.synchronize { @store.size } end |
#write(key, value, options = {}) ⇒ Object, false
Writes a value to the cache with an optional expiration time.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rack_jwt_verifier/in_process_cache.rb', line 43 def write(key, value, = {}) @lock.synchronize do return false if [:unless_exist] && !live_value(key).nil? expiry = [:expires_in] || DEFAULT_EXPIRY @store[key] = [value, @clock.call + expiry] sweep_if_needed value end end |