Faraday::Middleware
Public: Caches GET responses and pulls subsequent ones from the cache.
Public: initialize the middleware.
cache - An object that responds to read, write and fetch (default: nil). options - An options Hash (default: {}):
:ignore_params - String name or Array names of query params that should be ignored when forming the cache key (default: []).
Yields if no cache is given. The block should return a cache object.
# File lib/faraday_middleware/response/caching.rb, line 23 def initialize(app, cache = nil, options = {}) super(app) options, cache = cache, nil if cache.is_a? Hash and block_given? @cache = cache || yield @options = options end
# File lib/faraday_middleware/response/caching.rb, line 45 def cache_key(env) url = env[:url].dup if url.query && params_to_ignore.any? params = parse_query url.query params.reject! {|k,| params_to_ignore.include? k } url.query = build_query params end url.normalize! url.request_uri end
# File lib/faraday_middleware/response/caching.rb, line 60 def cache_on_complete(env) key = cache_key(env) if cached_response = cache.read(key) finalize_response(cached_response, env) else response = @app.call(env) response.on_complete { cache.write(key, response) } end end
# File lib/faraday_middleware/response/caching.rb, line 30 def call(env) if :get == env[:method] if env[:parallel_manager] # callback mode cache_on_complete(env) else # synchronous mode response = cache.fetch(cache_key(env)) { @app.call(env) } finalize_response(response, env) end else @app.call(env) end end
# File lib/faraday_middleware/response/caching.rb, line 70 def finalize_response(response, env) response = response.dup if response.frozen? env[:response] = response unless env[:response_headers] env.update response.env # FIXME: omg hax response.instance_variable_set('@env', env) end response end
Generated with the Darkfish Rdoc Generator 2.