Class: Faraday::HttpCache
- Inherits:
-
Middleware
- Object
- Middleware
- Faraday::HttpCache
- 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
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
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, Rails.cache
end
Defined Under Namespace
Classes: CacheControl, Response, Storage
Instance Method Summary collapse
-
#call(env) ⇒ Object
Public: Process the request into a duplicate of this instance to ensure that the internal state is preserved.
-
#call!(env) ⇒ Object
Internal: Process the stack request to try to serve a cache response.
-
#initialize(app, *arguments) ⇒ HttpCache
constructor
Public: Initializes a new HttpCache middleware.
Constructor Details
#initialize(app, *arguments) ⇒ HttpCache
Public: Initializes a new HttpCache middleware.
app - the next endpoint on the ‘Faraday’ stack. arguments - aditional options to setup the logger and the storage.
Examples:
# Initialize the middleware with a logger.
Faraday::HttpCache.new(app, :logger => my_logger)
# Initialize the middleware with a FileStore at the 'tmp' dir.
Faraday::HttpCache.new(app, :file_store, 'tmp')
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/faraday/http_cache.rb', line 51 def initialize(app, *arguments) super(app) if arguments.last.is_a? Hash = arguments.pop @logger = .delete(:logger) else = arguments end store = arguments.shift @storage = Storage.new(store, ) 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.
68 69 70 |
# File 'lib/faraday/http_cache.rb', line 68 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.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/faraday/http_cache.rb', line 82 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 |