Class: Hurley::HttpCache
- Inherits:
-
Object
- Object
- Hurley::HttpCache
- Defined in:
- lib/hurley/http_cache.rb,
lib/hurley/http_cache/request.rb,
lib/hurley/http_cache/storage.rb,
lib/hurley/http_cache/response.rb,
lib/hurley/http_cache/cache_control.rb
Defined Under Namespace
Classes: CacheControl, MemoryStore, Request, Response, Storage
Constant Summary collapse
- UNSAFE_VERBS =
[:post, :put, :delete, :patch]
- ERROR_STATUSES =
400..499
Instance Method Summary collapse
-
#call(request) ⇒ Object
Public: Process the client request to try to serve a cache response.
-
#initialize(connection = nil, shared_cache: true, serializer: nil, store: nil, logger: nil) ⇒ HttpCache
constructor
Public: Initialize a Hurley connection that supports HTTP caching.
Constructor Details
#initialize(connection = nil, shared_cache: true, serializer: nil, store: nil, logger: nil) ⇒ HttpCache
Public: Initialize a Hurley connection that supports HTTP caching.
connection - A real connection object that can make HTTP requests
(default: the Hurley.default_connection value).
shared_cache - A flag to indicate if the connection should act as a shared
cache or not (default: true)
serializer - A serializer object for the cache store (default: nil). store - A cache store to receive the stored requests and responses
(default: nil).
logger - A Logger object (default: nil).
Examples
# Initialize the connection with the Rails logger. client = Hurley.new client.connection = Hurley::HttpCache.new(logger: Rails.logger)
# Initialize the connection with the Hurley Excon connection adapter. require ‘hurley-excon’
client = Hurley.new client.connection = Hurley::HttpCache.new(HurleyExcon::Connection.new)
35 36 37 38 39 40 41 |
# File 'lib/hurley/http_cache.rb', line 35 def initialize(connection = nil, shared_cache: true, serializer: nil, store: nil, logger: nil) @connection = connection || Hurley.default_connection @logger = logger @shared_cache = shared_cache @storage = Storage.new(store: store, serializer: serializer, logger: logger) end |
Instance Method Details
#call(request) ⇒ Object
Public: Process the client request to try to serve a cache response. On a cacheable request, we will attempt to locate a valid stored response to serve. On a cache miss, we will forward the request and try to store the response for future calls. If the request can’t be cached, the request will be delegated directly to the underlying connection and does nothing to the response. The processed steps will be recorded to be logged once the whole process is finished.
request - The incoming Hurley::Request object.
Returns a Hurley::Response object.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/hurley/http_cache.rb', line 55 def call(request) @trace = [] request = Hurley::HttpCache::Request.new(request) if request.cacheable? if request.no_cache? response = bypass(request) else response = process(request) end else trace :unacceptable response = forward(request) end delete(request, response) if should_delete?(response.status_code, request.verb) log_request(request) response end |