Class: Faraday::HttpCache::Storage
- Inherits:
-
Object
- Object
- Faraday::HttpCache::Storage
- 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
-
#cache ⇒ Object
readonly
Public: Gets the underlying cache store object.
Instance Method Summary collapse
- #delete(url) ⇒ Object
-
#initialize(options = {}) ⇒ Storage
constructor
Internal: Initialize a new Storage object with a cache backend.
-
#read(request, klass = Faraday::HttpCache::Response) ⇒ Object
Internal: Attempt to retrieve an stored response that suits the incoming HTTP request.
-
#write(request, response) ⇒ Object
Internal: Store a response inside the cache.
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 'dump' and 'load'.
: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( = {}) @cache = [:store] || MemoryStore.new @serializer = [:serializer] || JSON @logger = [:logger] assert_valid_store! end |
Instance Attribute Details
#cache ⇒ Object (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
80 81 82 83 |
# File 'lib/faraday/http_cache/storage.rb', line 80 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’.
70 71 72 73 74 75 76 77 78 |
# File 'lib/faraday/http_cache/storage.rb', line 70 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 |
# 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.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.}. Try using Marshal to serialize." raise e end |