Class: Interceptors::IdempotencyInterceptor
- Inherits:
-
Interceptor
- Object
- Interceptor
- Interceptors::IdempotencyInterceptor
- Defined in:
- lib/interceptors/idempotency_interceptor.rb
Defined Under Namespace
Classes: MemoryStore
Constant Summary collapse
- DEFAULT_STORE =
MemoryStore.new
Instance Method Summary collapse
- #around(ctx) ⇒ Object
-
#initialize(key_proc:, ttl: 300, store: DEFAULT_STORE) ⇒ IdempotencyInterceptor
constructor
A new instance of IdempotencyInterceptor.
Methods inherited from Interceptor
Constructor Details
#initialize(key_proc:, ttl: 300, store: DEFAULT_STORE) ⇒ IdempotencyInterceptor
Returns a new instance of IdempotencyInterceptor.
33 34 35 36 37 38 39 |
# File 'lib/interceptors/idempotency_interceptor.rb', line 33 def initialize(key_proc:, ttl: 300, store: DEFAULT_STORE) raise ArgumentError, "key_proc must be callable" unless key_proc.respond_to?(:call) @key_proc = key_proc @ttl = ttl.to_i @store = store end |
Instance Method Details
#around(ctx) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/interceptors/idempotency_interceptor.rb', line 41 def around(ctx) key = safe_key(ctx) return yield ctx unless key cached = @store.read(key) return cached if fresh?(cached) result = yield ctx return result unless result.is_a?(Result) return result unless result.ok? stored = result.(stored_at: Time.now.to_i) @store.write(key, stored, ttl: @ttl) stored end |