Class: Hurley::HttpCache::Storage
- Inherits:
-
Object
- Object
- Hurley::HttpCache::Storage
- Defined in:
- lib/hurley/http_cache/storage.rb
Overview
Internal: A Storage class to manage the caching of request and responses.
Examples
# Create a Storage object using Rails default cache store.
Hurley::HttpCache::Storage.new(store: Rails.cache)
# Create a new Storage using Marshal for serialization.
Hurley::HttpCache::Storage.new(store: Rails.cache, serializer: Marshal)
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Public: Get 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) ⇒ 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 ‘dump’ and
'load'.
:serializer - A serializer object that should respond to ‘dump’ and
'load'.
28 29 30 31 32 33 |
# File 'lib/hurley/http_cache/storage.rb', line 28 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: Get the underlying cache store object.
19 20 21 |
# File 'lib/hurley/http_cache/storage.rb', line 19 def cache @cache end |
Instance Method Details
#delete(url) ⇒ Object
70 71 72 73 |
# File 'lib/hurley/http_cache/storage.rb', line 70 def delete(url) cache_key = cache_key_for(url) cache.delete(cache_key) end |
#read(request) ⇒ Object
Internal: Attempt to retrieve an stored response that suits the incoming HTTP request.
request - A Hurley::HttpCache::::Request instance of the incoming HTTP
request.
Returns a Hash.
64 65 66 67 68 |
# File 'lib/hurley/http_cache/storage.rb', line 64 def read(request) cache_key = cache_key_for(request.url) entry = read_entry(cache_key) lookup_response(request, entry) end |
#write(request, response) ⇒ Object
Internal: Store a response inside the cache. Existing entries for the exact same request will be removed from the cache and replaced by the given response object.
request - A Hurley::HttpCache::::Request instance of the executed HTTP
request.
response - The Hurley::HttpCache::Response instance to be stored.
Returns nothing.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/hurley/http_cache/storage.rb', line 44 def write(request, response) key = cache_key_for(request.url) entry = read_entry(key) || [] entry.reject! { |(req, res)| request.matches?(req, res) } entry << [request.serializable_hash, response.serializable_hash] write_entry(key, entry) rescue Encoding::UndefinedConversionError => e warn { "Response could not be serialized: #{e.}. Try using Marshal to serialize." } raise e end |