class Restforce::Middleware::Caching
Public: Caches GET responses and pulls subsequent ones from the cache.
def cache_key(env)
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 = params.any? ? build_query(params) : nil end url.normalize! digest = full_key? ? url.host + url.request_uri : url.request_uri Digest::SHA1.hexdigest(digest) end
def cache_on_complete(env)
def cache_on_complete(env) key = cache_key(env) if (cached_response = cache.read(key)) finalize_response(cached_response, env) else # response.status is nil at this point # any checks need to be done inside on_complete block @app.call(env).on_complete do |response_env| store_response_in_cache(key, response_env.response) response_env end end end
def call(env)
def call(env) # Taken from `Restforce::Middleware::Caching` implementation # before we switched away from the `faraday_middleware` gem. # See https://github.com/restforce/restforce/blob/a08b9d6c5e277bd7111ffa7ed50465dd49c05fab/lib/restforce/middleware/caching.rb. cache&.delete(cache_key(env)) unless use_cache? if env[:method] == :get if env[:parallel_manager] # callback mode cache_on_complete(env) else # synchronous mode key = cache_key(env) unless (response = cache.read(key)) && response response = @app.call(env) store_response_in_cache(key, response) end finalize_response(response, env) end else @app.call(env) end end
def custom_status_codes
def custom_status_codes @custom_status_codes ||= begin codes = CACHEABLE_STATUS_CODES & Array(@options[:status_codes]).map(&:to_i) codes.any? ? codes : CACHEABLE_STATUS_CODES end end
def finalize_response(response, env)
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
def full_key?
def full_key? @full_key ||= @options[:full_key] end
def initialize(app, cache = nil, options = {})
(default: CACHEABLE_STATUS_CODE)
:status_codes - Array of http status code to be cache
(url.host + url.request_uri)
:full_key - Boolean - use full URL as cache key:
will be passed.
method. If not specified, no options parameter
third options parameter to the cache's #write
:write_options - Hash of settings that should be passed as the
the cache key (default: []).
params that should be ignored when forming
:ignore_params - String name or Array names of query
options - An options Hash (default: {}):
cache - An object that responds to read and write (default: nil).
Public: initialize the middleware.
def initialize(app, cache = nil, options = {}) super(app) if cache.is_a?(Hash) && block_given? options = cache cache = nil end @cache = cache || yield @options = options end
def params_to_ignore
def params_to_ignore @params_to_ignore ||= Array(@options[:ignore_params]).map(&:to_s) end
def store_response_in_cache(key, response)
def store_response_in_cache(key, response) return unless custom_status_codes.include?(response.status) if @options[:write_options] cache.write(key, response, @options[:write_options]) else cache.write(key, response) end end
def use_cache?
before we switched away from the `faraday_middleware` gem.
Taken from `Restforce::Middleware::Caching` implementation
def use_cache? @options[:use_cache] end