Class: Faraday::HttpCache::Storage
- Inherits:
-
Object
- Object
- Faraday::HttpCache::Storage
- Defined in:
- lib/faraday/http_cache/storage.rb
Overview
Internal: A Wrapper around a 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 a ActiveSupport::CacheStore 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
Returns the value of attribute cache.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Storage
constructor
Internal: Initialize a new Storage object with a cache backend.
-
#read(request, klass = Faraday::HttpCache::Response) ⇒ Object
Internal: Reads a key based on the given request from the underlying cache.
-
#write(request, response) ⇒ Object
Internal: Writes a response with a key based on the given request.
Constructor Details
#initialize(options = {}) ⇒ Storage
Internal: Initialize a new Storage object with a cache backend.
options - Storage options (default: {}).
:store - An ActiveSupport::CacheStore identifier.
:serializer - A serializer class for the body.
Should respond to #dump and #load.
:store_options - An array containg the options for
the cache store
29 30 31 32 33 |
# File 'lib/faraday/http_cache/storage.rb', line 29 def initialize( = {}) store = [:store] @serializer = [:serializer] || MultiJson @cache = ActiveSupport::Cache.lookup_store(store, [:store_options]) end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
19 20 21 |
# File 'lib/faraday/http_cache/storage.rb', line 19 def cache @cache end |
Instance Method Details
#read(request, klass = Faraday::HttpCache::Response) ⇒ Object
Internal: Reads a key based on the given request from the underlying cache.
request - The Hash containing the request information.
:method - The HTTP Method used for the request.
:url - The requested URL.
:request_headers - The custom headers for the request.
klass - The Class to be instantiated with the recovered informations.
55 56 57 58 59 60 61 62 63 |
# File 'lib/faraday/http_cache/storage.rb', line 55 def read(request, klass = Faraday::HttpCache::Response) key = cache_key_for(request) value = cache.read(key) if value payload = @serializer.load(value).symbolize_keys klass.new(payload) end end |
#write(request, response) ⇒ Object
Internal: Writes a response with a key based on the given request.
request - The Hash containing the request information.
:method - The HTTP Method used for the request.
:url - The requested URL.
:request_headers - The custom headers for the request.
response - The Faraday::HttpCache::Response instance to be stored.
42 43 44 45 46 |
# File 'lib/faraday/http_cache/storage.rb', line 42 def write(request, response) key = cache_key_for(request) value = @serializer.dump(response.serializable_hash) cache.write(key, value) end |