Class: Faraday::HttpCache::Strategies::ByUrl

Inherits:
BaseStrategy show all
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

Faraday::HttpCache::Storage

Instance Attribute Summary

Attributes inherited from BaseStrategy

#cache

Instance Method Summary collapse

Methods inherited from BaseStrategy

#initialize

Constructor Details

This class inherits a constructor from Faraday::HttpCache::Strategies::BaseStrategy

Instance Method Details

#delete(url) ⇒ void

This method returns an undefined value.

Parameters:

  • url (String)

    – the url of a changed resource, will be used to invalidate the cache.



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.

Parameters:

Returns:



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.

Parameters:



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.message}. Try using Marshal to serialize."
  raise e
end