Class: HTTP::Cache

### Home Classes Methods

### In Files

- lib/http/cache.rb
- lib/http/cache/headers.rb
- lib/http/cache/null_cache.rb

### Parent

Object

### Namespace

- CLASS HTTP::Cache::Headers
- CLASS HTTP::Cache::NullCache
- CLASS HTTP::Cache::NullLogger

### Methods

- ::new
- #cache_lookup
- #get_response
- #handle_response
- #invalidate_cache
- #make_request
- #perform
- #storage
- #store_in_cache
- #stringify

### Class/Module Index

Quicksearch

- HTTP
- HTTP::Cache
- HTTP::Cache::Headers
- HTTP::Cache::NullCache
- HTTP::Cache::NullLogger
- HTTP::CacheError
- HTTP::Chainable
- HTTP::Client
- HTTP::Connection
- HTTP::Error
- HTTP::Headers
- HTTP::Headers::Mixin
- HTTP::InvalidHeaderNameError
- HTTP::MimeType
- HTTP::MimeType::Adapter
- HTTP::MimeType::JSON
- HTTP::Options
- HTTP::Redirector
- HTTP::Redirector::EndlessRedirectError
- HTTP::Redirector::TooManyRedirectsError
- HTTP::Request
- HTTP::Request::Caching
- HTTP::Request::UnsupportedMethodError
- HTTP::Request::UnsupportedSchemeError
- HTTP::Request::Writer
- HTTP::RequestError
- HTTP::Response
- HTTP::Response::Body
- HTTP::Response::Caching
- HTTP::Response::IoBody
- HTTP::Response::Parser
- HTTP::Response::Status
- HTTP::Response::StringBody
- HTTP::ResponseError
- HTTP::StateError
- HTTP::Timeout
- HTTP::Timeout::Global
- HTTP::Timeout::Null
- HTTP::Timeout::PerOperation
- HTTP::TimeoutError
- HTTP::URI
- Object

No matching classes.

# HTTP::Cache

### Attributes

entitystore[R]
logger[R]
metastore[R]

### Public Class Methods

new(opts) click to toggle source

Inits a new instance

@option opts [String] :metastore URL to the metastore location @option opts [String] :entitystore URL to the entitystore location @option opts [Logger] :logger logger to use

    # File lib/http/cache.rb, line 122
    def initialize(opts)
      @metastore   = storage.resolve_metastore_uri(opts.fetch(:metastore))
      @entitystore = storage.resolve_entitystore_uri(opts.fetch(:entitystore))
      @logger = opts.fetch(:logger) { NullLogger.new }
    end

### Public Instance Methods

perform(request, options, &request_performer) click to toggle source

@return [Response] a cached response that is valid for the request or

    the result of executing the provided block

@yield [request, options] on cache miss so that an actual request can be made

    # File lib/http/cache.rb, line 26
    def perform(request, options, &request_performer)
      req = request.caching

      invalidate_cache(req) if req.invalidates_cache?

      get_response(req, options, request_performer)
    end

### Protected Instance Methods

cache_lookup(request) click to toggle source

@return [HTTP::Response::Caching, nil] the cached response for the request

    # File lib/http/cache.rb, line 84
    def cache_lookup(request)
      return nil if request.skips_cache?

      rack_resp = metastore.lookup(request, entitystore)
      return if rack_resp.nil?

      HTTP::Response.new(
        rack_resp.status, "1.1", rack_resp.headers, stringify(rack_resp.body)
      ).caching
    end

get_response(req, options, request_performer) click to toggle source

@return [Response] the response to the request, either from the cache or by actually making the request

    # File lib/http/cache.rb, line 38
    def get_response(req, options, request_performer)
      cached_resp = cache_lookup(req)
      return cached_resp if cached_resp && !cached_resp.stale?

      # cache miss
      logger.debug { "Cache miss for <#{req.uri}>, making request" }
      actual_req = if cached_resp
                     req.conditional_on_changes_to(cached_resp)
                   else
                     req
                   end
      actual_resp = make_request(actual_req, options, request_performer)

      handle_response(cached_resp, actual_resp, req)
    end

handle_response(cached_resp, actual_resp, req) click to toggle source

@return [Response] the most useful of the responses after

    updating the cache as appropriate

    # File lib/http/cache.rb, line 56
    def handle_response(cached_resp, actual_resp, req)
      if actual_resp.status.not_modified? && cached_resp
        logger.debug { "<#{req.uri}> not modified, using cached version." }
        cached_resp.validated!(actual_resp)
        store_in_cache(req, cached_resp)
        return cached_resp

      elsif req.cacheable? && actual_resp.cacheable?
        store_in_cache(req, actual_resp)
        return actual_resp

      else
        return actual_resp
      end
    end

invalidate_cache(request) click to toggle source

Invalidate all response from the requested resource

@return [nil]

    # File lib/http/cache.rb, line 113
    def invalidate_cache(request)
      metastore.invalidate(request, entitystore)
    end

make_request(req, options, request_performer) click to toggle source

@return [HTTP::Response::Caching] the actual response returned by request_performer

    # File lib/http/cache.rb, line 74
    def make_request(req, options, request_performer)
      req.sent_at = Time.now

      request_performer.call(req, options).caching.tap do |res|
        res.received_at  = Time.now
        res.requested_at = req.sent_at
      end
    end

storage() click to toggle source

    # File lib/http/cache.rb, line 130
    def storage
      @@storage ||= Rack::Cache::Storage.new # rubocop:disable Style/ClassVars
    end

store_in_cache(request, response) click to toggle source

Store response in cache

@return [nil]

We have to convert the response body in to a string body so that the cache store reading the body will not prevent the original requester from doing so.

    # File lib/http/cache.rb, line 104
    def store_in_cache(request, response)
      response.body = response.body.to_s
      metastore.store(request, response, entitystore)
      nil
    end

stringify(body) click to toggle source

    # File lib/http/cache.rb, line 134
    def stringify(body)
      if body.respond_to?(:each)
        "".tap do |buf|
          body.each do |part|
            buf << part
          end
        end
      else
        body.to_s
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.