Class: Idempo::ActiveRecordBackend::Store

Inherits:
Struct
  • Object
show all
Defined in:
lib/idempo/active_record_backend.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keyObject

Returns the value of attribute key

Returns:

  • (Object)

    the current value of key



12
13
14
# File 'lib/idempo/active_record_backend.rb', line 12

def key
  @key
end

#modelObject

Returns the value of attribute model

Returns:

  • (Object)

    the current value of model



12
13
14
# File 'lib/idempo/active_record_backend.rb', line 12

def model
  @model
end

Instance Method Details

#lookupObject



13
14
15
# File 'lib/idempo/active_record_backend.rb', line 13

def lookup
  model.where(idempotent_request_key: key).where("expire_at > ?", Time.now).first&.idempotent_response_payload
end

#store(data:, ttl:) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/idempo/active_record_backend.rb', line 17

def store(data:, ttl:)
  # MySQL does not support datetime with subsecont precision, so ceil() it is
  expire_at = Time.now.utc + ttl.ceil
  model.transaction do
    model.where(idempotent_request_key: key).delete_all
    model.create(idempotent_request_key: key, idempotent_response_payload: data, expire_at: expire_at)
  end
  true
end