Module: HTTPX::Plugins::ResponseCache

Defined in:
lib/httpx/plugins/response_cache.rb,
lib/httpx/plugins/response_cache/store.rb,
lib/httpx/plugins/response_cache/file_store.rb

Overview

This plugin adds support for retrying requests when certain errors happen.

gitlab.com/os85/httpx/wikis/Response-Cache

Defined Under Namespace

Modules: InstanceMethods, OptionsMethods, RequestMethods, ResponseMethods Classes: FileStore, Store

Class Method Summary collapse

Class Method Details

.cacheable_request?(request) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
# File 'lib/httpx/plugins/response_cache.rb', line 21

def cacheable_request?(request)
  CACHEABLE_VERBS.include?(request.verb) &&
    (
      !request.headers.key?("cache-control") || !request.headers.get("cache-control").include?("no-store")
    )
end

.cacheable_response?(response) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/httpx/plugins/response_cache.rb', line 28

def cacheable_response?(response)
  response.is_a?(Response) &&
    (
      response.cache_control.nil? ||
      # TODO: !response.cache_control.include?("private") && is shared cache
      !response.cache_control.include?("no-store")
    ) &&
    CACHEABLE_STATUS_CODES.include?(response.status) &&
    # RFC 2616 13.4 - A response received with a status code of 200, 203, 206, 300, 301 or
    # 410 MAY be stored by a cache and used in reply to a subsequent
    # request, subject to the expiration mechanism, unless a cache-control
    # directive prohibits caching. However, a cache that does not support
    # the Range and Content-Range headers MUST NOT cache 206 (Partial
    # Content) responses.
    response.status != 206 && (
    response.headers.key?("etag") || response.headers.key?("last-modified") || response.fresh?
  )
end

.cached_response?(response) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/httpx/plugins/response_cache.rb', line 47

def cached_response?(response)
  response.is_a?(Response) && response.status == 304
end

.extra_options(options) ⇒ Object



51
52
53
# File 'lib/httpx/plugins/response_cache.rb', line 51

def extra_options(options)
  options.merge(response_cache_store: Store.new)
end

.load_dependenciesObject



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

def load_dependencies(*)
  require_relative "response_cache/store"
end