Class: Wrest::CacheProxy::DefaultCacheProxy
- Inherits:
-
Object
- Object
- Wrest::CacheProxy::DefaultCacheProxy
- Defined in:
- lib/wrest/cache_proxy.rb
Constant Summary collapse
- HOP_BY_HOP_HEADERS =
["connection", "keep-alive", "proxy-authenticate", "proxy-authorization", "te", "trailers", "transfer-encoding", "upgrade"]
Instance Method Summary collapse
- #cache(response) ⇒ Object
- #get ⇒ Object
-
#get_fresh_response ⇒ Object
:nodoc:.
-
#get_validated_response_for(cached_response) ⇒ Object
:nodoc:.
-
#initialize(get, cache_store) ⇒ DefaultCacheProxy
constructor
A new instance of DefaultCacheProxy.
- #log_cached_response ⇒ Object
-
#send_validation_request_for(cached_response) ⇒ Object
:nodoc: Send a cache-validation request to the server.
- #update_cache_headers_for(cached_response, new_response) ⇒ Object
Constructor Details
#initialize(get, cache_store) ⇒ DefaultCacheProxy
Returns a new instance of DefaultCacheProxy.
33 34 35 36 |
# File 'lib/wrest/cache_proxy.rb', line 33 def initialize(get, cache_store) @get = get @cache_store = cache_store end |
Instance Method Details
#cache(response) ⇒ Object
63 64 65 |
# File 'lib/wrest/cache_proxy.rb', line 63 def cache(response) @cache_store[@get.full_uri_string] = response.clone if response && response.cacheable? end |
#get ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/wrest/cache_proxy.rb', line 42 def get cached_response = @cache_store[@get.full_uri_string] return get_fresh_response if cached_response.nil? if cached_response.expired? if cached_response.can_be_validated? get_validated_response_for(cached_response) else get_fresh_response end else log_cached_response cached_response end end |
#get_fresh_response ⇒ Object
:nodoc:
68 69 70 71 72 73 74 75 76 |
# File 'lib/wrest/cache_proxy.rb', line 68 def get_fresh_response @cache_store.delete @get.full_uri_string response = @get.invoke_without_cache_check cache(response) response end |
#get_validated_response_for(cached_response) ⇒ Object
:nodoc:
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/wrest/cache_proxy.rb', line 79 def get_validated_response_for(cached_response) new_response = send_validation_request_for(cached_response) if new_response.code == "304" update_cache_headers_for(cached_response, new_response) log_cached_response cached_response else cache(new_response) new_response end end |
#log_cached_response ⇒ Object
38 39 40 |
# File 'lib/wrest/cache_proxy.rb', line 38 def log_cached_response Wrest.logger.debug "<*> (GET #{@get.hash}) #{@get.uri.protocol}://#{@get.uri.host}:#{@get.uri.port}#{@get.http_request.path}" end |
#send_validation_request_for(cached_response) ⇒ Object
:nodoc: Send a cache-validation request to the server. This would be the actual Get request with extra cache-validation headers. If a 304 (Not Modified) is received, Wrest would use the cached_response itself. Otherwise the new response is cached and used.
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/wrest/cache_proxy.rb', line 94 def send_validation_request_for(cached_response) last_modified = cached_response.last_modified etag = cached_response.headers["etag"] cache_validation_headers = {} cache_validation_headers["if-modified-since"] = last_modified unless last_modified.nil? cache_validation_headers["if-none-match"] = etag unless etag.nil? new_request = @get.build_request_without_cache_store(cache_validation_headers) new_request.invoke end |
#update_cache_headers_for(cached_response, new_response) ⇒ Object
58 59 60 61 |
# File 'lib/wrest/cache_proxy.rb', line 58 def update_cache_headers_for(cached_response, new_response) # RFC 2616 13.5.3 (Combining Headers) cached_response.headers.merge!(new_response.headers.select {|key, value| not (HOP_BY_HOP_HEADERS.include? key.downcase)}) end |