Class: Redmine::HttpCaching
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Redmine::HttpCaching
- Defined in:
- lib/redmine/http_caching.rb
Overview
A decorator object for RestClient that adds caching capabilities to regular RestClient objects. It wraps normal request methods and uses the Etag and If-None-Match headers to locally store responses. On subsequent requests for the same resource, we can skip downloading the entire body when the server indicates nothing has changed (Not Modified).
A cache is used to keep track of responses. This is assumed to support the Pstore interface, responding to #transaction, #[] and #[]= methods.
Usage example:
require 'pstore'
cache = PStore.new('cache.pstore')
rest_client = RestClient.new
caching_rest_client = HttpCaching.new(rest_client, cache)
caching_rest_client.get('/path')
Instance Method Summary collapse
-
#get(path, headers = {}) ⇒ Object
Wrap RestClient#get to provide HTTP caching.
-
#initialize(http, cache) ⇒ HttpCaching
constructor
A new instance of HttpCaching.
Constructor Details
#initialize(http, cache) ⇒ HttpCaching
21 22 23 24 |
# File 'lib/redmine/http_caching.rb', line 21 def initialize(http, cache) @cache = cache super(http) end |
Instance Method Details
#get(path, headers = {}) ⇒ Object
Wrap RestClient#get to provide HTTP caching.
New requests are stored in a cache if they have an Etag header. On subsequent requests to the same resource, a If-None-Match header is sent along, using the original Etag value. The server can then indicate that nothing has changed, which will trigger this decorator to return the cached response – rather than downloading a whole new copy of the same data.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/redmine/http_caching.rb', line 34 def get(path, headers = {}) cached_response = fetch_cached_response(path) if cached_response headers = headers.merge 'If-None-Match' => cached_response['Etag'] end response = super(path, headers) case response when Net::HTTPNotModified then cached_response else cache_response(path, response) response end end |