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

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

Direct Known Subclasses

FileStore

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



6
7
8
9
# File 'lib/httpx/plugins/response_cache/store.rb', line 6

def initialize
  @store = {}
  @store_mutex = Thread::Mutex.new
end

Instance Method Details

#cache(request, response) ⇒ Object



27
28
29
30
31
# File 'lib/httpx/plugins/response_cache/store.rb', line 27

def cache(request, response)
  return unless ResponseCache.cacheable_request?(request) && ResponseCache.cacheable_response?(response)

  _set(request, response)
end

#cached?(request) ⇒ Boolean

Returns:

  • (Boolean)


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

def cached?(request)
  lookup(request)
end

#clearObject



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

def clear
  @store_mutex.synchronize { @store.clear }
end

#lookup(request) ⇒ Object



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

def lookup(request)
  responses = _get(request)

  return unless responses

  responses.find(&method(:match_by_vary?).curry(2)[request])
end

#prepare(request) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/httpx/plugins/response_cache/store.rb', line 33

def prepare(request)
  cached_response = lookup(request)

  return unless cached_response

  return unless match_by_vary?(request, cached_response)

  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