Class: Routemaster::Middleware::ResponseCaching

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

Constant Summary collapse

KEY_TEMPLATE =
'cache:{url}'
BODY_FIELD_TEMPLATE =
'v:{version},l:{locale},body'
HEADERS_FIELD_TEMPLATE =
'v:{version},l:{locale},headers'
VERSION_REGEX =
/application\/json;v=(?<version>\S*)/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, cache: Config.cache_redis, listener: nil) ⇒ ResponseCaching

Returns a new instance of ResponseCaching.



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

def initialize(app, cache: Config.cache_redis, listener: nil)
  @app = app
  @cache = cache
  @expiry  = Config.cache_expiry
  @listener = listener
end

Instance Method Details

#call(env) ⇒ Object



18
19
20
21
22
# File 'lib/routemaster/middleware/response_caching.rb', line 18

def call(env)
  return @app.call(env) unless env.method == :get

  fetch_from_cache(env) || fetch_from_service(env)
end