Class: Faraday::HttpCache::Strategies::BaseStrategy Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/faraday/http_cache/strategies/base_strategy.rb

Overview

This class is abstract.

Base class for all strategies.

Examples:


# 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)

Direct Known Subclasses

ByUrl, ByVary

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BaseStrategy

Returns a new instance of BaseStrategy.

Parameters:

  • options (Hash) (defaults to: {})

    the options to create a message with.

Options Hash (options):

  • :store (Faraday::HttpCache::MemoryStore, nil)
    • a cache

    store object that should respond to ‘read’, ‘write’, and ‘delete’.

  • :serializer (#dump#load)
    • an object that should

    respond to ‘dump’ and ‘load’.

  • :logger (Logger, nil)
    • an object to be used to emit warnings.



34
35
36
37
38
39
40
# 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

Instance Attribute Details

#cacheObject (readonly)

Returns the underlying cache store object.



26
27
28
# File 'lib/faraday/http_cache/strategies/base_strategy.rb', line 26

def cache
  @cache
end

Instance Method Details

#delete(_url) ⇒ Object

This method is abstract.

Delete responses from the cache by the url.

Raises:

  • (NotImplementedError)


56
57
58
# 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) ⇒ Object

This method is abstract.

Read a response from the cache.

Raises:

  • (NotImplementedError)


50
51
52
# 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) ⇒ Object

This method is abstract.

Store a response inside the cache.

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/faraday/http_cache/strategies/base_strategy.rb', line 44

def write(_request, _response)
  raise NotImplementedError, 'Implement this method in your strategy'
end