Class: Routemaster::Middleware::ResponseCaching

Inherits:
Object
  • Object
show all
Defined in:
lib/routemaster/middleware/response_caching.rb

Constant Summary collapse

BODY_FIELD_TEMPLATE =
'v:{version},l:{locale},body'.freeze
HEADERS_FIELD_TEMPLATE =
'v:{version},l:{locale},headers'.freeze
VERSION_REGEX =
/application\/json;v=(?<version>\S*)/
RESPONSE_CACHING_OPT_HEADER =
'X-routemaster_drain.opt_cache'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ ResponseCaching

Returns a new instance of ResponseCaching.



13
14
15
16
17
18
# File 'lib/routemaster/middleware/response_caching.rb', line 13

def initialize(app, options = {})
  @app = app
  @cache = options.fetch(:cache) { Config.cache_redis }
  @expiry = Config.cache_expiry
  @listener = options[:listener]
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
# File 'lib/routemaster/middleware/response_caching.rb', line 20

def call(env)
  @cache.del(cache_key(env)) if %i(patch delete).include?(env.method)
  return @app.call(env) if env.method != :get
  fetch_from_cache(env) || fetch_from_service(env, event_index(env))
end