Class Wrest::CacheProxy::DefaultCacheProxy

  1. lib/wrest/cache_proxy.rb
Parent: Object

Constants

HOP_BY_HOP_HEADERS = ["connection", "keep-alive", "proxy-authenticate", "proxy-authorization", "te", "trailers", "transfer-encoding", "upgrade"]

Public class methods

new (get, cache_store)
[show source]
    # File lib/wrest/cache_proxy.rb, line 33
33:       def initialize(get, cache_store)
34:         @get         = get
35:         @cache_store = cache_store
36:       end

Public instance methods

cache (response)
[show source]
    # File lib/wrest/cache_proxy.rb, line 63
63:       def cache(response)
64:         @cache_store[@get.hash] = response.clone if response && response.cacheable?
65:       end
get ()
[show source]
    # File lib/wrest/cache_proxy.rb, line 42
42:       def get
43:         cached_response = @cache_store[@get.hash]
44:         return get_fresh_response if cached_response.nil?
45: 
46:         if cached_response.expired?
47:           if cached_response.can_be_validated?
48:             get_validated_response_for(cached_response)
49:           else
50:             get_fresh_response
51:           end
52:         else
53:           log_cached_response
54:           cached_response
55:         end
56:       end
get_fresh_response ()

:nodoc:

[show source]
    # File lib/wrest/cache_proxy.rb, line 68
68:       def get_fresh_response
69:         @cache_store.delete @get.hash
70: 
71:         response = @get.invoke_without_cache_check
72: 
73:         cache(response)
74: 
75:         response
76:       end
get_validated_response_for (cached_response)

:nodoc:

[show source]
    # File lib/wrest/cache_proxy.rb, line 79
79:       def get_validated_response_for(cached_response)
80:         new_response = send_validation_request_for(cached_response)
81:         if new_response.code == "304"
82:           update_cache_headers_for(cached_response, new_response)
83:           log_cached_response
84:           cached_response
85:         else
86:           cache(new_response)
87:           new_response
88:         end
89:       end
log_cached_response ()
[show source]
    # File lib/wrest/cache_proxy.rb, line 38
38:       def log_cached_response
39:           Wrest.logger.debug "<*> (GET #{@get.hash}) #{@get.uri.protocol}://#{@get.uri.host}:#{@get.uri.port}#{@get.http_request.path}"
40:       end
send_validation_request_for (cached_response)

: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.
[show source]
     # File lib/wrest/cache_proxy.rb, line 94
 94:       def send_validation_request_for(cached_response)
 95:         last_modified            = cached_response.last_modified
 96:         etag                     = cached_response.headers["etag"]
 97: 
 98:         cache_validation_headers = {}
 99:         cache_validation_headers["if-modified-since"] = last_modified unless last_modified.nil?
100:         cache_validation_headers["if-none-match"] = etag unless etag.nil?
101: 
102:         new_request = @get.build_request_without_cache_store(cache_validation_headers)
103: 
104:         new_request.invoke
105:       end
update_cache_headers_for (cached_response, new_response)
[show source]
    # File lib/wrest/cache_proxy.rb, line 58
58:       def update_cache_headers_for(cached_response, new_response)
59:         # RFC 2616 13.5.3 (Combining Headers)
60:         cached_response.headers.merge!(new_response.headers.select {|key, value| not (HOP_BY_HOP_HEADERS.include? key.downcase)})
61:       end