Class: Gitlab::EtagCaching::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/etag_caching/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.


6
7
8
# File 'lib/gitlab/etag_caching/middleware.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gitlab/etag_caching/middleware.rb', line 10

def call(env)
  request = ActionDispatch::Request.new(env)
  route = Gitlab::EtagCaching::Router.match(request.path_info)
  return @app.call(env) unless route

  track_event(:etag_caching_middleware_used, route)

  etag, cached_value_present = get_etag(request)
  if_none_match = env['HTTP_IF_NONE_MATCH']

  if if_none_match == etag
    handle_cache_hit(etag, route, request)
  else
    track_cache_miss(if_none_match, cached_value_present, route)

    status, headers, body = @app.call(env)
    headers['ETag'] = etag
    [status, headers, body]
  end
end