Class: GarageClient::Cachers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/garage_client/cachers/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Base

Returns a new instance of Base.



5
6
7
# File 'lib/garage_client/cachers/base.rb', line 5

def initialize(env)
  @env = env
end

Instance Method Details

#callObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/garage_client/cachers/base.rb', line 9

def call
  response = store.read(key, options) if read_from_cache?
  if response
    # Faraday::Response#marshal_dump is drop request object and url
    # https://github.com/lostisland/faraday/blob/edacd5eb57ea13accab3097649690ae5f48f421a/lib/faraday/response.rb#L74
    #
    # XXX: We can't use #merge! here because Faraday::Env does not implement
    # the method as same as Hash#merge! with Faraday v0.12.1.
    @env.each do |k, v|
      original = response.env[k]
      response.env[k] = v if !original && v
    end
  else
    response = yield
    store.write(key, response, options) if written_to_cache?
  end
  response
end