Module: HTTPX::Plugins::ResponseCache

Defined in:
lib/httpx/plugins/response_cache.rb,
sig/plugins/response_cache.rbs

Overview

This plugin caches and reuses responses based on HTTP caching directives defined by the HTTP Caching RFC

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

Defined Under Namespace

Modules: InstanceMethods, OptionsMethods, RequestMethods, ResponseMethods, _ResponseCacheOptions

Constant Summary collapse

CACHEABLE_VERBS =

Returns:

  • (Array[verb])
%w[GET HEAD].freeze
CACHEABLE_STATUS_CODES =

Returns:

  • (Array[Integer])
[200, 203, 206, 300, 301, 410].freeze
SUPPORTED_VARY_HEADERS =

Returns:

  • (Array[String])
%w[accept accept-encoding accept-language cookie origin].sort.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cacheable_response?(response) ⇒ Boolean

whether the response can be stored in the response cache. (i.e. has a cacheable body, does not contain directives prohibiting storage, etc...)

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/httpx/plugins/response_cache.rb', line 25

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
end

.extra_options(options) ⇒ Object



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

def extra_options(options)
  options.merge(
    supported_vary_headers: SUPPORTED_VARY_HEADERS,
  )
end

.load_dependencies(klass) ⇒ Object



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

def load_dependencies(klass)
  klass.plugin(:cache)
end

.not_modified?(response) ⇒ Boolean

whether the response

Returns:

  • (Boolean)


43
44
45
# File 'lib/httpx/plugins/response_cache.rb', line 43

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

Instance Method Details

#self?.cacheable_response?Boolean

Parameters:

Returns:

  • (Boolean)


8
# File 'sig/plugins/response_cache.rbs', line 8

def self?.cacheable_response?: (::HTTPX::ErrorResponse | responseCacheResponse response) -> bool

#self?.not_modified?Boolean

Parameters:

  • response (response)

Returns:

  • (Boolean)


10
# File 'sig/plugins/response_cache.rbs', line 10

def self?.not_modified?: (response response) -> bool