Class: Faraday::ManualCache

Inherits:
Middleware
  • Object
show all
Defined in:
lib/faraday/manual_cache.rb

Overview

Middleware for caching Faraday responses based on a specified expiry.

As with faraday-http-cache, it’s recommended that this middleware be added fairly low in the middleware stack.

Currently accepts four arguments:

:conditions    - Conditional caching based on a lambda (default: GET/HEAD
                 requests)
:expires_in    - Cache expiry, in seconds (default: 30).
:logger        - A logger object to send cache hit/miss/write messages.

Constant Summary collapse

DEFAULT_CONDITIONS =
->(env) { env.method == :get || env.method == :head }
DEFAULT_CACHE_KEY =
->(env) { env.url }

Instance Method Summary collapse

Constructor Details

#initialize(app, *args) ⇒ ManualCache

Returns a new instance of ManualCache.



21
22
23
24
25
26
27
28
# File 'lib/faraday/manual_cache.rb', line 21

def initialize(app, *args)
  super(app)
  options = args.first || {}
  @conditions    = options.fetch(:conditions, DEFAULT_CONDITIONS)
  @expires_in    = options.fetch(:expires_in, 30)
  @logger        = options.fetch(:logger, nil)
  @cache_key     = options.fetch(:cache_key, DEFAULT_CACHE_KEY)
end

Instance Method Details

#call(env) ⇒ Object



30
31
32
# File 'lib/faraday/manual_cache.rb', line 30

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