Class: Faraday::HttpCache

Inherits:
Middleware
  • Object
show all
Defined in:
lib/faraday/http_cache.rb,
lib/faraday/http_cache/request.rb,
lib/faraday/http_cache/storage.rb,
lib/faraday/http_cache/response.rb,
lib/faraday/http_cache/cache_control.rb

Overview

Public: The middleware responsible for caching and serving responses. The middleware use the provided configuration options to establish a 'Faraday::HttpCache::Storage' to cache responses retrieved by the stack adapter. If a stored response can be served again for a subsequent request, the middleware will return the response instead of issuing a new request to it's server. This middleware should be the last attached handler to your stack, so it will be closest to the inner app, avoiding issues with other middlewares on your stack.

Examples:

# Using the middleware with a simple client:
client = Faraday.new do |builder|
  builder.use :http_cache, store: my_store_backend
  builder.adapter Faraday.default_adapter
end

# Attach a Logger to the middleware.
client = Faraday.new do |builder|
  builder.use :http_cache, logger: my_logger_instance, store: my_store_backend
  builder.adapter Faraday.default_adapter
end

# Provide an existing CacheStore (for instance, from a Rails app)
client = Faraday.new do |builder|
  builder.use :http_cache, store: Rails.cache
end

# Use Marshal for serialization
client = Faraday.new do |builder|
  builder.use :http_cache, store: Rails.cache, serializer: Marshal
end

# Instrument events using ActiveSupport::Notifications
client = Faraday.new do |builder|
  builder.use :http_cache, store: Rails.cache, instrumenter: ActiveSupport::Notifications
end

Defined Under Namespace

Classes: CacheControl, MemoryStore, Request, Response, Storage

Constant Summary collapse

UNSAFE_METHODS =
[:post, :put, :delete, :patch].freeze
ERROR_STATUSES =
(400..499).freeze
EVENT_NAME =

The name of the instrumentation event.

'http_cache.faraday'
CACHE_STATUSES =
[
  # The request was not cacheable.
  :unacceptable,

  # The response was cached and can still be used.
  :fresh,

  # The response was cached and the server has validated it with a 304 response.
  :valid,

  # The response was cached but was not revalidated by the server.
  :invalid,

  # No response was found in the cache.
  :miss,

  # The response can't be cached.
  :uncacheable,

  # The request was cached but need to be revalidated by the server.
  :must_revalidate,
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, store: nil, serializer: nil, shared_cache: true, instrumenter: nil, instrument_name: EVENT_NAME, logger: nil) ⇒ HttpCache

Public: Initializes a new HttpCache middleware.

app - the next endpoint on the 'Faraday' stack. :store - A cache store that should respond to 'read', 'write', and 'delete'. :serializer - A serializer that should respond to 'dump' and 'load'. :shared_cache - A flag to mark the middleware as a shared cache or not. :instrumenter - An instrumentation object that should respond to 'instrument'. :instrument_name - The String name of the instrument being reported on (optional). :logger - A logger object.

Examples:

# Initialize the middleware with a logger.
Faraday::HttpCache.new(app, logger: my_logger)

# Initialize the middleware with a logger and Marshal as a serializer
Faraday:HttpCache.new(app, logger: my_logger, serializer: Marshal)

# Initialize the middleware with a FileStore at the 'tmp' dir.
store = ActiveSupport::Cache.lookup_store(:file_store, ['tmp'])
Faraday::HttpCache.new(app, store: store)

# Initialize the middleware with a MemoryStore and logger
store = ActiveSupport::Cache.lookup_store
Faraday::HttpCache.new(app, store: store, logger: my_logger)

102
103
104
105
106
107
108
109
110
# File 'lib/faraday/http_cache.rb', line 102

def initialize(app, store: nil, serializer: nil, shared_cache: true, instrumenter: nil, instrument_name: EVENT_NAME, logger: nil) # rubocop:disable Metrics/ParameterLists
  super(app)

  @logger = logger
  @shared_cache = shared_cache
  @instrumenter = instrumenter
  @instrument_name = instrument_name
  @storage = Storage.new(store: store, serializer: serializer, logger: logger)
end

Instance Method Details

#call(env) ⇒ Object

Public: Process the request into a duplicate of this instance to ensure that the internal state is preserved.


114
115
116
# File 'lib/faraday/http_cache.rb', line 114

def call(env)
  dup.call!(env)
end

#call!(env) ⇒ Object

Internal: Process the stack request to try to serve a cache response. On a cacheable request, the middleware will attempt to locate a valid stored response to serve. On a cache miss, the middleware will forward the request and try to store the response for future requests. If the request can't be cached, the request will be delegated directly to the underlying app and does nothing to the response. The processed steps will be recorded to be logged once the whole process is finished.

Returns a 'Faraday::Response' instance.


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/faraday/http_cache.rb', line 128

def call!(env)
  @trace = []
  @request = create_request(env)

  response = nil

  if @request.cacheable?
    response = process(env)
  else
    trace :unacceptable
    response = @app.call(env)
  end

  response.on_complete do
    delete(@request, response) if should_delete?(response.status, @request.method)
    log_request
    response.env[:http_cache_trace] = @trace
    instrument(response.env)
  end
end