class Faraday::ManualCache

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.

Constants

DEFAULT_CACHE_KEY
DEFAULT_CONDITIONS

Public Class Methods

new(app, *args) click to toggle source
Calls superclass method
# 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

Public Instance Methods

call(env) click to toggle source
# File lib/faraday/manual_cache.rb, line 30
def call(env)
  dup.call!(env)
end

Protected Instance Methods

cache_response(env) click to toggle source
# File lib/faraday/manual_cache.rb, line 50
def cache_response(env)
  return unless cacheable?(env) && !env.request_headers['x-faraday-manual-cache']

  info "Cache WRITE: #{key(env)}"
  store.write(
    key(env),
    env,
    expires_in: expires_in(env)
  )
end
cacheable?(env) click to toggle source
# File lib/faraday/manual_cache.rb, line 61
def cacheable?(env)
  @conditions.call(env)
end
cached_response(env) click to toggle source
# File lib/faraday/manual_cache.rb, line 65
def cached_response(env)
  if cacheable?(env) && !env.request_headers['x-faraday-manual-cache']
    response_env = store.fetch(key(env))
  end

  if response_env
    info "Cache HIT: #{key(env)}"
  else
    info "Cache MISS: #{key(env)}"
  end

  response_env
end
call!(env) click to toggle source
# File lib/faraday/manual_cache.rb, line 36
def call!(env)
  response_env = cached_response(env)

  if response_env
    response_env.response_headers['x-faraday-manual-cache'] = 'HIT'
    to_response(response_env)
  else
    @app.call(env).on_complete do |response_env|
      response_env.response_headers['x-faraday-manual-cache'] = 'MISS'
      cache_response(response_env)
    end
  end
end
expires_in(env) click to toggle source
# File lib/faraday/manual_cache.rb, line 87
def expires_in(env)
  @expires_in.respond_to?(:call) ? @expires_in.call(env) : @expires_in
end
info(message) click to toggle source
# File lib/faraday/manual_cache.rb, line 79
def info(message)
  @logger.info(message) unless @logger.nil?
end
key(env) click to toggle source
# File lib/faraday/manual_cache.rb, line 83
def key(env)
  @cache_key.call(env)
end
store() click to toggle source
# File lib/faraday/manual_cache.rb, line 91
def store
  @store ||= FaradayManualCache.configuration.memory_store
end
to_response(env) click to toggle source
# File lib/faraday/manual_cache.rb, line 95
def to_response(env)
  env = env.dup
  env.response_headers['x-faraday-manual-cache'] = 'HIT'
  response = Response.new
  response.finish(env) unless env.parallel?
  env.response = response
end