Class: Faraday::HttpCache

Inherits:
Middleware
  • Object
show all
Defined in:
lib/faraday/http_cache.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.user :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

Defined Under Namespace

Classes: CacheControl, MemoryStore, Response, Storage

Constant Summary collapse

VALID_OPTIONS =

Internal: valid options for the ‘initialize’ configuration Hash.

[:store, :serializer, :logger, :store_options, :shared_cache]

Instance Method Summary collapse

Constructor Details

#initialize(app, *args) ⇒ HttpCache

Public: Initializes a new HttpCache middleware.

app - the next endpoint on the ‘Faraday’ stack. args - aditional options to setup the logger and the storage.

:logger        - A logger object.
:serializer    - A serializer that should respond to 'dump' and 'load'.
:shared_cache  - A flag to mark the middleware as a shared cache or not.
:store         - A cache store that should respond to 'read' and 'write'.
:store_options - Deprecated: additional options to setup the cache store.

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)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/faraday/http_cache.rb', line 68

def initialize(app, *args)
  super(app)
  @logger = nil
  @shared_cache = true
  if args.first.is_a? Hash
    options = args.first
    @logger = options[:logger]
    @shared_cache = options.fetch(:shared_cache, true)
  else
    options = parse_deprecated_options(*args)
  end

  assert_valid_options!(options)
  @storage = Storage.new(options)
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.



86
87
88
# File 'lib/faraday/http_cache.rb', line 86

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.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/faraday/http_cache.rb', line 100

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

  response = nil

  if can_cache?(@request[:method])
    response = process(env)
  else
    trace :unacceptable
    response = @app.call(env)
  end

  response.on_complete do
    log_request
  end
end

#shared_cache?Boolean

Internal: Should this cache instance act like a “shared cache” according to the the definition in RFC 2616?

Returns:

  • (Boolean)


120
121
122
# File 'lib/faraday/http_cache.rb', line 120

def shared_cache?
  @shared_cache
end