class Faraday::HttpCache::Storage
Faraday::HttpCache::Storage.new(:memory_store, serializer: Marshal)
# Creates a new Storage using Marshal for serialization.
Faraday::HttpCache::Storage.new(Rails.cache)
# Reuse some other instance of a ActiveSupport::CacheStore object.
Faraday::HttpCache::Storage.new(:mem_cache_store)
# Creates a new Storage using a MemCached backend from ActiveSupport.
Examples
Internal: A Wrapper around an ActiveSupport::CacheStore to store responses.
def assert_valid_store!
Raises an 'ArgumentError'.
expect API ('read' and 'write').
Internal: Checks if the given cache object supports the
def assert_valid_store! unless @cache.respond_to?(:read) && @cache.respond_to?(:write) raise ArgumentError.new("#{@cache.inspect} is not a valid cache store as it does not responds to 'read' and 'write'.") end end
def cache_key_for(request)
string.
on hashes order on Ruby 1.8), encoded as JSON and digested as a `SHA1`
The request object is folded into a sorted Array (since we can't count
Internal: Generates a String key for a given request object.
def cache_key_for(request) cache_keys = request.each_with_object([]) do |(key, value), parts| parts << [key.to_s, value] end Digest::SHA1.hexdigest(@serializer.dump(cache_keys.sort)) end
def initialize(options = {})
:store_options - An array containg the options for
respond to 'dump' and 'load'.
:serializer - A serializer object that should
respond to 'dump' and 'load'.
:store - An cache store object that should
:logger - A Logger object to be used to emit warnings.
options - Storage options (default: {}).
Internal: Initialize a new Storage object with a cache backend.
def initialize(options = {}) @cache = options[:store] || MemoryStore.new @serializer = options[:serializer] || JSON @logger = options[:logger] if @cache.is_a? Symbol @cache = lookup_store(@cache, options[:store_options]) end assert_valid_store! notify_memory_store_usage end
def lookup_store(store, options)
options - Additional options for the cache store.
store - A 'Symbol' with the store name.
Internal: Creates a cache store from 'ActiveSupport' with a set of options.
def lookup_store(store, options) if @logger @logger.warn "Passing a Symbol as the 'store' is deprecated, please pass the cache store instead." end begin require 'active_support/cache' ActiveSupport::Cache.lookup_store(store, options) rescue LoadError => e puts "You're missing the 'activesupport' gem. Add it to your Gemfile, bundle it and try again" raise e end end
def notify_memory_store_usage
isn't suitable for production use.
Internal: Logs a warning when the 'cache' implementation
def notify_memory_store_usage return if @logger.nil? kind = @cache.class.name.split('::').last.sub('Store', '').downcase if kind == 'memory' @logger.warn 'HTTP Cache: using a MemoryStore is not advised as the cache might not be persisted across multiple processes or connection instances.' end end
def read(request, klass = Faraday::HttpCache::Response)
:request_headers - The custom headers for the request.
:url - The requested URL.
:method - The HTTP Method used for the request.
request - The Hash containing the request information.
Internal: Reads a key based on the given request from the underlying cache.
def read(request, klass = Faraday::HttpCache::Response) cache_key = cache_key_for(request) found = @cache.read(cache_key) if found payload = @serializer.load(found).each_with_object({}) do |(key,value), hash| hash[key.to_sym] = value end klass.new(payload) end end
def write(request, response)
:request_headers - The custom headers for the request.
:url - The requested URL.
:method - The HTTP Method used for the request.
request - The Hash containing the request information.
Internal: Writes a response with a key based on the given request.
def write(request, response) key = cache_key_for(request) value = @serializer.dump(response.serializable_hash) @cache.write(key, value) end