Class: Prefab::CachingHttpConnection
- Inherits:
-
Object
- Object
- Prefab::CachingHttpConnection
- Defined in:
- lib/prefab/caching_http_connection.rb
Defined Under Namespace
Classes: CacheEntry
Constant Summary collapse
- CACHE_SIZE =
2.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #get(path) ⇒ Object
-
#initialize(uri, api_key) ⇒ CachingHttpConnection
constructor
A new instance of CachingHttpConnection.
-
#post(path, body) ⇒ Object
Delegate other methods to the underlying connection.
- #uri ⇒ Object
Constructor Details
#initialize(uri, api_key) ⇒ CachingHttpConnection
Returns a new instance of CachingHttpConnection.
18 19 20 |
# File 'lib/prefab/caching_http_connection.rb', line 18 def initialize(uri, api_key) @connection = HttpConnection.new(uri, api_key) end |
Class Method Details
.cache ⇒ Object
9 10 11 |
# File 'lib/prefab/caching_http_connection.rb', line 9 def cache @cache ||= FixedSizeHash.new(CACHE_SIZE) end |
.reset_cache! ⇒ Object
13 14 15 |
# File 'lib/prefab/caching_http_connection.rb', line 13 def reset_cache! @cache = FixedSizeHash.new(CACHE_SIZE) end |
Instance Method Details
#get(path) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/prefab/caching_http_connection.rb', line 22 def get(path) now = Time.now.to_i cache_key = "#{@connection.uri}#{path}" cached = self.class.cache[cache_key] # Check if we have a valid cached response if cached&.data && cached.expires_at && now < cached.expires_at return Faraday::Response.new( status: 200, body: cached.data, response_headers: { 'ETag' => cached.etag, 'X-Cache' => 'HIT', 'X-Cache-Expires-At' => cached.expires_at.to_s } ) end # Make request with conditional GET if we have an ETag response = if cached&.etag @connection.get(path, { 'If-None-Match' => cached.etag }) else @connection.get(path) end # Handle 304 Not Modified if response.status == 304 && cached&.data return Faraday::Response.new( status: 200, body: cached.data, response_headers: { 'ETag' => cached.etag, 'X-Cache' => 'HIT', 'X-Cache-Expires-At' => cached.expires_at.to_s } ) end # Parse caching headers cache_control = response.headers['Cache-Control'].to_s etag = response.headers['ETag'] # Always add X-Cache header response.headers['X-Cache'] = 'MISS' # Don't cache if no-store is present return response if cache_control.include?('no-store') # Calculate expiration max_age = cache_control.match(/max-age=(\d+)/)&.captures&.first&.to_i expires_at = max_age ? now + max_age : nil # Cache the response if we have caching headers if etag || expires_at self.class.cache[cache_key] = CacheEntry.new( response.body, etag, expires_at ) end response end |
#post(path, body) ⇒ Object
Delegate other methods to the underlying connection
87 88 89 |
# File 'lib/prefab/caching_http_connection.rb', line 87 def post(path, body) @connection.post(path, body) end |
#uri ⇒ Object
91 92 93 |
# File 'lib/prefab/caching_http_connection.rb', line 91 def uri @connection.uri end |