Class: Faraday::HttpCache::Storage

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

Overview

Internal: A wrapper around an ActiveSupport::CacheStore to store responses.

Examples

# Creates a new Storage using a MemCached backend from ActiveSupport.
Faraday::HttpCache::Storage.new(:mem_cache_store)

# Reuse some other instance of an ActiveSupport::Cache::Store object.
Faraday::HttpCache::Storage.new(Rails.cache)

# Creates a new Storage using Marshal for serialization.
Faraday::HttpCache::Storage.new(:memory_store, serializer: Marshal)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Storage

Internal: Initialize a new Storage object with a cache backend.

options - Storage options (default: {}).

:logger        - A Logger object to be used to emit warnings.
:store         - An cache store object that should
                 respond to 'read', 'write', and 'delete'.
:serializer    - A serializer object that should
                 respond to 'dump' and 'load'.


30
31
32
33
34
35
# File 'lib/faraday/http_cache/storage.rb', line 30

def initialize(options = {})
  @cache = options[:store] || MemoryStore.new
  @serializer = options[:serializer] || JSON
  @logger = options[:logger]
  assert_valid_store!
end

Instance Attribute Details

#cacheObject (readonly)

Public: Gets the underlying cache store object.



20
21
22
# File 'lib/faraday/http_cache/storage.rb', line 20

def cache
  @cache
end

Instance Method Details

#delete(url) ⇒ Object



81
82
83
84
# File 'lib/faraday/http_cache/storage.rb', line 81

def delete(url)
  cache_key = cache_key_for(url)
  cache.delete(cache_key)
end

#read(request, klass = Faraday::HttpCache::Response) ⇒ Object

Internal: Attempt to retrieve an stored response that suits the incoming HTTP request.

request - A Faraday::HttpCache::::Request instance of the incoming HTTP

request.

klass - The Class to be instantiated with the stored response.

Returns an instance of ‘klass’.



71
72
73
74
75
76
77
78
79
# File 'lib/faraday/http_cache/storage.rb', line 71

def read(request, klass = Faraday::HttpCache::Response)
  cache_key = cache_key_for(request.url)
  entries = cache.read(cache_key)
  response = lookup_response(request, entries)

  if response
    klass.new(response)
  end
end

#write(request, response) ⇒ Object

Internal: Store a response inside the cache.

request - A Faraday::HttpCache::::Request instance of the executed HTTP

request.

response - The Faraday::HttpCache::Response instance to be stored.

Returns nothing.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/faraday/http_cache/storage.rb', line 44

def write(request, response)
  key = cache_key_for(request.url)
  entry = serialize_entry(request.serializable_hash, response.serializable_hash)

  entries = cache.read(key) || []
  entries = entries.dup if entries.frozen?

  entries.reject! do |(cached_request, cached_response)|
    response_matches?(request, deserialize_object(cached_request), deserialize_object(cached_response))
  end

  entries << entry

  cache.write(key, entries)
rescue Encoding::UndefinedConversionError => e
  warn "Response could not be serialized: #{e.message}. Try using Marshal to serialize."
  raise e
end