Class: Gitlab::EtagCaching::Middleware

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

Constant Summary collapse

SKIP_HEADER_KEY =
'X-Gitlab-Skip-Etag'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



14
15
16
# File 'lib/gitlab/etag_caching/middleware.rb', line 14

def initialize(app)
  @app = app
end

Class Method Details

.skip!(response) ⇒ Object



9
10
11
# File 'lib/gitlab/etag_caching/middleware.rb', line 9

def skip!(response)
  response.set_header(SKIP_HEADER_KEY, '1')
end

Instance Method Details

#call(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gitlab/etag_caching/middleware.rb', line 18

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

  track_event(:etag_caching_middleware_used, route)

  etag, cached_value_present = get_etag(request, route)
  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)

    maybe_apply_etag(etag, *@app.call(env))
  end
end