Class: HTTPX::Plugins::ResponseCache::Store

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/httpx/plugins/response_cache/store.rb

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



12
13
14
# File 'lib/httpx/plugins/response_cache/store.rb', line 12

def initialize
  @store = {}
end

Instance Method Details

#cache(uri, response) ⇒ Object



24
25
26
# File 'lib/httpx/plugins/response_cache/store.rb', line 24

def cache(uri, response)
  @store[uri] = response
end

#cached?(uri) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/httpx/plugins/response_cache/store.rb', line 20

def cached?(uri)
  @store.key?(uri)
end

#lookup(uri) ⇒ Object



16
17
18
# File 'lib/httpx/plugins/response_cache/store.rb', line 16

def lookup(uri)
  @store[uri]
end

#prepare(request) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/httpx/plugins/response_cache/store.rb', line 28

def prepare(request)
  cached_response = @store[request.uri]

  return unless cached_response

  original_request = cached_response.instance_variable_get(:@request)

  if (vary = cached_response.headers["vary"])
    if vary == "*"
      return unless request.headers.same_headers?(original_request.headers)
    else
      return unless vary.split(/ *, */).all? do |cache_field|
        !original_request.headers.key?(cache_field) || request.headers[cache_field] == original_request.headers[cache_field]
      end
    end
  end

  if !request.headers.key?("if-modified-since") && (last_modified = cached_response.headers["last-modified"])
    request.headers.add("if-modified-since", last_modified)
  end

  if !request.headers.key?("if-none-match") && (etag = cached_response.headers["etag"]) # rubocop:disable Style/GuardClause
    request.headers.add("if-none-match", etag)
  end
end