Sha256: ff35a8b673722b2969060fc932703c613559fefbdfc4eb301c419b680a2da21a

Contents?: true

Size: 1.41 KB

Versions: 17

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require "forwardable"

module HTTPX::Plugins
  module ResponseCache
    class Store
      extend Forwardable

      def_delegator :@store, :clear

      def initialize
        @store = {}
      end

      def lookup(uri)
        @store[uri]
      end

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

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

      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
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
httpx-0.19.8 lib/httpx/plugins/response_cache/store.rb
httpx-0.19.7 lib/httpx/plugins/response_cache/store.rb
httpx-0.19.6 lib/httpx/plugins/response_cache/store.rb
httpx-0.19.5 lib/httpx/plugins/response_cache/store.rb
httpx-0.19.4 lib/httpx/plugins/response_cache/store.rb
httpx-0.19.3 lib/httpx/plugins/response_cache/store.rb
httpx-0.19.2 lib/httpx/plugins/response_cache/store.rb
httpx-0.19.1 lib/httpx/plugins/response_cache/store.rb
httpx-0.19.0 lib/httpx/plugins/response_cache/store.rb
httpx-0.18.7 lib/httpx/plugins/response_cache/store.rb
httpx-0.18.6 lib/httpx/plugins/response_cache/store.rb
httpx-0.18.5 lib/httpx/plugins/response_cache/store.rb
httpx-0.18.4 lib/httpx/plugins/response_cache/store.rb
httpx-0.18.3 lib/httpx/plugins/response_cache/store.rb
httpx-0.18.2 lib/httpx/plugins/response_cache/store.rb
httpx-0.18.1 lib/httpx/plugins/response_cache/store.rb
httpx-0.18.0 lib/httpx/plugins/response_cache/store.rb