Module: HTTPX::Plugins::ResponseCache
- Defined in:
- lib/httpx/plugins/response_cache.rb
Overview
This plugin caches and reuses responses based on HTTP caching directives defined by the [HTTP Caching RFC](www.rfc-editor.org/rfc/rfc9111.html)
Defined Under Namespace
Modules: InstanceMethods, OptionsMethods, RequestMethods, ResponseMethods
Constant Summary collapse
- SUPPORTED_VARY_HEADERS =
%w[accept accept-encoding accept-language cookie origin].sort.freeze
Class Method Summary collapse
-
.cacheable_response?(response) ⇒ Boolean
whether the
responsecan be stored in the response cache. - .extra_options(options) ⇒ Object
- .load_dependencies(klass) ⇒ Object
-
.not_modified?(response) ⇒ Boolean
whether the
response.
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…)
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 () .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
43 44 45 |
# File 'lib/httpx/plugins/response_cache.rb', line 43 def not_modified?(response) response.is_a?(Response) && response.status == 304 end |