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)
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
Instance Method Summary collapse
-
#initialize(store = nil, 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(store = nil, options = {}) ⇒ Storage
Internal: Initialize a new Storage object with a cache backend.
store - An ActiveSupport::CacheStore identifier (default: nil). options - The Hash options for the CacheStore backend (default: {}).
22 23 24 |
# File 'lib/faraday/http_cache/storage.rb', line 22 def initialize(store = nil, = {}) @cache = ActiveSupport::Cache.lookup_store(store, ) end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
16 17 18 |
# File 'lib/faraday/http_cache/storage.rb', line 16 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.
46 47 48 49 50 51 52 53 54 |
# File 'lib/faraday/http_cache/storage.rb', line 46 def read(request, klass = Faraday::HttpCache::Response) key = cache_key_for(request) value = cache.read(key) if value payload = MultiJson.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.
33 34 35 36 37 |
# File 'lib/faraday/http_cache/storage.rb', line 33 def write(request, response) key = cache_key_for(request) value = MultiJson.dump(response.serializable_hash) cache.write(key, value) end |