Class: Wrest::CacheProxy::DefaultCacheProxy
- Inherits:
-
Object
- Object
- Wrest::CacheProxy::DefaultCacheProxy
- Defined in:
- lib/wrest/cache_proxy.rb
Constant Summary collapse
- HOP_BY_HOP_HEADERS =
%w[connection keep-alive proxy-authenticate proxy-authorization te trailers transfer-encoding upgrade].freeze
Instance Method Summary collapse
- #cache(response) ⇒ Object
-
#fresh_get_response ⇒ Object
:nodoc:.
- #get ⇒ Object
-
#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.
35 36 37 38 |
# File 'lib/wrest/cache_proxy.rb', line 35 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&.cacheable? end |
#fresh_get_response ⇒ Object
:nodoc:
68 69 70 71 72 73 74 75 76 |
# File 'lib/wrest/cache_proxy.rb', line 68 def fresh_get_response @cache_store.delete @get.full_uri_string response = @get.invoke_without_cache_check cache(response) response end |
#get ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/wrest/cache_proxy.rb', line 44 def get cached_response = @cache_store[@get.full_uri_string] return fresh_get_response if cached_response.nil? if cached_response.expired? expired_cached_response(cached_response) else log_cached_response cached_response end 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
40 41 42 |
# File 'lib/wrest/cache_proxy.rb', line 40 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
56 57 58 59 60 61 |
# File 'lib/wrest/cache_proxy.rb', line 56 def update_cache_headers_for(cached_response, new_response) # RFC 2616 13.5.3 (Combining Headers) cached_response.headers.merge!(new_response.headers.reject do |key, _value| (HOP_BY_HOP_HEADERS.include? key.downcase) end) end |