Class: Faraday::HttpCache::Strategies::ByUrl
- Inherits:
-
BaseStrategy
- Object
- BaseStrategy
- Faraday::HttpCache::Strategies::ByUrl
- Defined in:
- lib/faraday/http_cache/strategies/by_url.rb
Overview
The original strategy by Faraday::HttpCache. Uses URL + HTTP method to generate cache keys.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from BaseStrategy
Instance Method Summary collapse
- #delete(url) ⇒ void
-
#read(request) ⇒ Faraday::HttpCache::Response?
Fetch a stored response that suits the incoming HTTP request or return nil.
-
#write(request, response) ⇒ void
Store a response inside the cache.
Methods inherited from BaseStrategy
Constructor Details
This class inherits a constructor from Faraday::HttpCache::Strategies::BaseStrategy
Instance Method Details
#delete(url) ⇒ void
This method returns an undefined value.
53 54 55 56 |
# File 'lib/faraday/http_cache/strategies/by_url.rb', line 53 def delete(url) cache_key = cache_key_for(url) cache.delete(cache_key) end |
#read(request) ⇒ Faraday::HttpCache::Response?
Fetch a stored response that suits the incoming HTTP request or return nil.
41 42 43 44 45 46 47 48 |
# File 'lib/faraday/http_cache/strategies/by_url.rb', line 41 def read(request) cache_key = cache_key_for(request.url) entries = cache.read(cache_key) response = lookup_response(request, entries) return nil unless response Faraday::HttpCache::Response.new(response) end |
#write(request, response) ⇒ void
This method returns an undefined value.
Store a response inside the cache.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/faraday/http_cache/strategies/by_url.rb', line 19 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 |