class Faraday::HttpCache::Strategies::BaseStrategy
Base class for all strategies. @abstract
@example
# Creates a new strategy using a MemCached backend from ActiveSupport. mem_cache_store = ActiveSupport::Cache.lookup_store(:mem_cache_store, ['localhost:11211']) Faraday::HttpCache::Strategies::ByVary.new(store: mem_cache_store) # Reuse some other instance of an ActiveSupport::Cache::Store object. Faraday::HttpCache::Strategies::ByVary.new(store: Rails.cache) # Creates a new strategy using Marshal for serialization. Faraday::HttpCache::Strategies::ByVary.new(store: Rails.cache, serializer: Marshal)
Attributes
cache[R]
Returns the underlying cache store object.
Public Class Methods
new(options = {})
click to toggle source
@param [Hash] options the options to create a message with. @option options [Faraday::HttpCache::MemoryStore, nil] :store - a cache
store object that should respond to 'read', 'write', and 'delete'.
@option options [#dump#load] :serializer - an object that should
respond to 'dump' and 'load'.
@option options [Logger, nil] :logger - an object to be used to emit warnings.
# File lib/faraday/http_cache/strategies/base_strategy.rb, line 34 def initialize(options = {}) @cache = options[:store] || Faraday::HttpCache::MemoryStore.new @serializer = options[:serializer] || JSON @logger = options[:logger] || Logger.new(IO::NULL) @cache_salt = (@serializer.is_a?(Module) ? @serializer : @serializer.class).name assert_valid_store! end
Public Instance Methods
delete(_url)
click to toggle source
Delete responses from the cache by the url. @abstract
# File lib/faraday/http_cache/strategies/base_strategy.rb, line 56 def delete(_url) raise NotImplementedError, 'Implement this method in your strategy' end
read(_request)
click to toggle source
Read a response from the cache. @abstract
# File lib/faraday/http_cache/strategies/base_strategy.rb, line 50 def read(_request) raise NotImplementedError, 'Implement this method in your strategy' end
write(_request, _response)
click to toggle source
Store a response inside the cache. @abstract
# File lib/faraday/http_cache/strategies/base_strategy.rb, line 44 def write(_request, _response) raise NotImplementedError, 'Implement this method in your strategy' end
Private Instance Methods
assert_valid_store!()
click to toggle source
@private @raise [ArgumentError] if the cache object doesn’t support the expect API.
# File lib/faraday/http_cache/strategies/base_strategy.rb, line 64 def assert_valid_store! unless cache.respond_to?(:read) && cache.respond_to?(:write) && cache.respond_to?(:delete) raise ArgumentError.new("#{cache.inspect} is not a valid cache store as it does not responds to 'read', 'write' or 'delete'.") end end
deserialize_entry(*objects)
click to toggle source
# File lib/faraday/http_cache/strategies/base_strategy.rb, line 78 def deserialize_entry(*objects) objects.map { |object| deserialize_object(object) } end
deserialize_object(object)
click to toggle source
# File lib/faraday/http_cache/strategies/base_strategy.rb, line 82 def deserialize_object(object) @serializer.load(object).each_with_object({}) do |(key, value), hash| hash[key.to_sym] = value end end
serialize_entry(*objects)
click to toggle source
# File lib/faraday/http_cache/strategies/base_strategy.rb, line 70 def serialize_entry(*objects) objects.map { |object| serialize_object(object) } end
serialize_object(object)
click to toggle source
# File lib/faraday/http_cache/strategies/base_strategy.rb, line 74 def serialize_object(object) @serializer.dump(object) end
warn(message)
click to toggle source
# File lib/faraday/http_cache/strategies/base_strategy.rb, line 88 def warn(message) @logger.warn(message) end