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.
mem_cache_store = ActiveSupport::Cache.lookup_store(:mem_cache_store, ['localhost:11211'])
Faraday::HttpCache::Storage.new(store: mem_cache_store)
# Reuse some other instance of an ActiveSupport::Cache::Store object.
Faraday::HttpCache::Storage.new(store: Rails.cache)
# Creates a new Storage using Marshal for serialization.
Faraday::HttpCache::Storage.new(store: Rails.cache, serializer: Marshal)
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Public: Gets the underlying cache store object.
Instance Method Summary collapse
- #delete(url) ⇒ Object
-
#initialize(store: nil, serializer: nil, logger: nil) ⇒ 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(store: nil, serializer: nil, logger: nil) ⇒ Storage
Internal: Initialize a new Storage object with a cache backend.
: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'.
31 32 33 34 35 36 |
# File 'lib/faraday/http_cache/storage.rb', line 31 def initialize(store: nil, serializer: nil, logger: nil) @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.
22 23 24 |
# File 'lib/faraday/http_cache/storage.rb', line 22 def cache @cache end |
Instance Method Details
#delete(url) ⇒ Object
82 83 84 85 |
# File 'lib/faraday/http_cache/storage.rb', line 82 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’.
72 73 74 75 76 77 78 79 80 |
# File 'lib/faraday/http_cache/storage.rb', line 72 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.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/faraday/http_cache/storage.rb', line 45 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.}. Try using Marshal to serialize." raise e end |