Class: Async::HTTP::Cache::Store::Vary

Inherits:
Object
  • Object
show all
Defined in:
lib/async/http/cache/store/vary.rb

Overview

Represents a cache store wrapper that handles HTTP Vary header functionality.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegate, vary = {}) ⇒ Vary

Initialize a new Vary store wrapper.



18
19
20
21
# File 'lib/async/http/cache/store/vary.rb', line 18

def initialize(delegate, vary = {})
  @delegate = delegate
  @vary = vary
end

Instance Attribute Details

#delegateObject (readonly)

Returns the value of attribute delegate.



28
29
30
# File 'lib/async/http/cache/store/vary.rb', line 28

def delegate
  @delegate
end

Instance Method Details

#closeObject

Close the vary store and its delegate.



24
25
26
# File 'lib/async/http/cache/store/vary.rb', line 24

def close
  @delegate.close
end

#insert(key, request, response) ⇒ Object

Insert a response into the cache, handling vary headers appropriately.



67
68
69
70
71
72
73
74
75
# File 'lib/async/http/cache/store/vary.rb', line 67

def insert(key, request, response)
  if vary = response.headers[VARY]&.sort
    @vary[key] = vary
    
    key = key + key_for(request.headers, vary)
  end
  
  @delegate.insert(key, request, response)
end

#key_for(headers, vary) ⇒ Object

Generate vary-specific key components from request headers.



46
47
48
# File 'lib/async/http/cache/store/vary.rb', line 46

def key_for(headers, vary)
  vary.map{|key| headers[key]}
end

#lookup(key, request) ⇒ Object

Look up a cached response, accounting for vary headers.



54
55
56
57
58
59
60
61
# File 'lib/async/http/cache/store/vary.rb', line 54

def lookup(key, request)
  if vary = @vary[key]
    # We should provide user-supported normalization here:
    key = key + key_for(request.headers, vary)
  end
  
  return @delegate.lookup(key, request)
end

#normalize(request) ⇒ Object

Normalize request headers to reduce cache key variations.



32
33
34
35
36
37
38
39
40
# File 'lib/async/http/cache/store/vary.rb', line 32

def normalize(request)
  if accept_encoding = request.headers[ACCEPT_ENCODING]
    if accept_encoding.include?("gzip")
      request.headers.set(ACCEPT_ENCODING, "gzip")
    else
      request.headers.delete(ACCEPT_ENCODING)
    end
  end
end