rack-cache 1.11.0 → 1.13.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7e2ca34b16edeb06ab8ce7df32ad78b273b600cd950ce3b56d03b898bf00e53f
4
- data.tar.gz: 5d35c87987859feb36e5f24b3b065ac790c6dc4f3df0969e41aaa6f15819ab94
3
+ metadata.gz: 2d031263204dbdf38df4290dfcda6ebf48f349d69b6499a878e616a18f0ff914
4
+ data.tar.gz: a4153ac38a62c88f8bae3e760d1a903dfbb1ba641f086d1dbd32bca20a81d118
5
5
  SHA512:
6
- metadata.gz: 1d156a65178883329c11d00024f60af8d013cd4fe2a3def6d930c43d822c76dd1bd9b6d26d965209fbfdc038e79cff96a5efd973deadace39e0b83097c332ec5
7
- data.tar.gz: 9a3b6fdc70f24de95a8cc02d3d9180c5c59f277156605ddca2b295e56542f81b0433fb8da279d0f337d49d4352e0e2500be4a468fc75a61c5b13eadccf1b3cc7
6
+ metadata.gz: c627fb81cd7eee48a3d54d9a07942b8b62b1fd480d1a89bfc58a717fd400a8f7e2d8453c336e2090c9bed92f410bf1f31a3bbe6a49f42d7b262e0da81c98b55d
7
+ data.tar.gz: 9329f2fd9d67707fd984a0dc9c8fc9030687d3b08e3a1a34a4efc168e5840d97be4dc065a3af6e7516ca821d1cf285ef7a29562b3d84788e6ae200b19c86b7c9
data/CHANGES CHANGED
@@ -1,3 +1,11 @@
1
+ ## 1.12.0
2
+
3
+ * Add a fault_tolerant flag to fail-over to stale cache
4
+
5
+ ## 1.11.1
6
+
7
+ * when ignoring parts of the query, remove query in key when all params are ignored
8
+
1
9
  ## 1.11.0
2
10
 
3
11
  * Add a proc to allow ignoring parts of the query string in the key
data/README.md CHANGED
@@ -16,7 +16,7 @@ validation (Last-Modified, ETag) information:
16
16
 
17
17
  For more information about Rack::Cache features and usage, see:
18
18
 
19
- http://rtomayko.github.com/rack-cache/
19
+ https://rtomayko.github.com/rack-cache/
20
20
 
21
21
  Rack::Cache is not overly optimized for performance. The main goal of the
22
22
  project is to provide a portable, easy-to-configure, and standards-based
@@ -70,7 +70,7 @@ You should now see `Rack::Cache` listed in the middleware pipeline:
70
70
 
71
71
  rake middleware
72
72
 
73
- [more information](http://snippets.aktagon.com/snippets/302-how-to-setup-and-use-rack-cache-with-rails)
73
+ [more information](https://snippets.aktagon.com/snippets/302-how-to-setup-and-use-rack-cache-with-rails)
74
74
 
75
75
  Using with Dalli
76
76
  ----------------
@@ -161,10 +161,12 @@ module Rack::Cache
161
161
  end
162
162
 
163
163
  # Try to serve the response from cache. When a matching cache entry is
164
- # found and is fresh, use it as the response without forwarding any
165
- # request to the backend. When a matching cache entry is found but is
166
- # stale, attempt to #validate the entry with the backend using conditional
167
- # GET. When no matching cache entry is found, trigger #miss processing.
164
+ # found and is fresh, use it as the response without forwarding any request
165
+ # to the backend. When a matching cache entry is found but is stale, attempt
166
+ # to #validate the entry with the backend using conditional GET.
167
+ # If validation raises an exception and fault tolerant caching is enabled,
168
+ # serve the stale cache entry.
169
+ # When no matching cache entry is found, trigger miss processing.
168
170
  def lookup
169
171
  if @request.no_cache? && allow_reload?
170
172
  record :reload
@@ -183,7 +185,11 @@ module Rack::Cache
183
185
  entry
184
186
  else
185
187
  record :stale
186
- validate(entry)
188
+ if fault_tolerant?
189
+ validate_with_stale_cache_failover(entry)
190
+ else
191
+ validate(entry)
192
+ end
187
193
  end
188
194
  else
189
195
  record :miss
@@ -192,6 +198,17 @@ module Rack::Cache
192
198
  end
193
199
  end
194
200
 
201
+ # Returns stale cache on exception.
202
+ def validate_with_stale_cache_failover(entry)
203
+ validate(entry)
204
+ rescue => e
205
+ record :connnection_failed
206
+ age = entry.age.to_s
207
+ entry.headers['Age'] = age
208
+ record "Fail-over to stale cache data with age #{age} due to #{e.class.name}: #{e}"
209
+ entry
210
+ end
211
+
195
212
  # Validate that the cache entry is fresh. The original request is used
196
213
  # as a template for a conditional GET request with the backend.
197
214
  def validate(entry)
@@ -62,7 +62,7 @@ module Rack::Cache
62
62
  parts.sort!
63
63
  parts.reject!(&self.class.query_string_ignore)
64
64
  parts.map! { |k,v| "#{escape(k)}=#{escape(v)}" }
65
- parts.join('&')
65
+ parts.empty? ? nil : parts.join('&')
66
66
  end
67
67
  end
68
68
  end
@@ -34,7 +34,7 @@ module Rack::Cache
34
34
 
35
35
  # find a cached entry that matches the request.
36
36
  env = request.env
37
- match = entries.detect{|req,res| requests_match?(res['Vary'], env, req)}
37
+ match = entries.detect{ |req,res| requests_match?((res['Vary'] || res['vary']), env, req) }
38
38
  return nil if match.nil?
39
39
 
40
40
  _, res = match
@@ -90,13 +90,14 @@ module Rack::Cache
90
90
  # the list
91
91
  vary = response.vary
92
92
  entries =
93
- read(key).reject do |env,res|
94
- (vary == res['Vary']) &&
93
+ read(key).reject do |env, res|
94
+ (vary == (res['Vary'] || res['vary'])) &&
95
95
  requests_match?(vary, env, stored_env)
96
96
  end
97
97
 
98
98
  headers = persist_response(response)
99
- headers.delete 'Age'
99
+ headers.delete('Age')
100
+ headers.delete('age')
100
101
 
101
102
  entries.unshift [stored_env, headers]
102
103
  if request.env['rack-cache.use_native_ttl'] && response.fresh?
@@ -107,6 +107,10 @@ module Rack::Cache
107
107
  # be used.
108
108
  option_accessor :use_native_ttl
109
109
 
110
+ # Specifies whether to serve a request from a stale cache entry if
111
+ # the attempt to revalidate the cache entry raises an exception.
112
+ option_accessor :fault_tolerant
113
+
110
114
  # The underlying options Hash. During initialization (or outside of a
111
115
  # request), this is a default values Hash. During a request, this is the
112
116
  # Rack environment Hash. The default values Hash is merged in underneath
@@ -149,6 +153,7 @@ module Rack::Cache
149
153
  'rack-cache.allow_reload' => false,
150
154
  'rack-cache.allow_revalidate' => false,
151
155
  'rack-cache.use_native_ttl' => false,
156
+ 'rack-cache.fault_tolerant' => false,
152
157
  }
153
158
  self.options = options
154
159
  end
@@ -40,6 +40,7 @@ module Rack::Cache
40
40
  def initialize_copy(other)
41
41
  super
42
42
  @headers = other.headers.dup
43
+ @cache_control = nil
43
44
  end
44
45
 
45
46
  # Return the status, headers, and body in a three-tuple.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.11.0
4
+ version: 1.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Tomayko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-06 00:00:00.000000000 Z
11
+ date: 2021-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -163,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
163
  - !ruby/object:Gem::Version
164
164
  version: '0'
165
165
  requirements: []
166
- rubygems_version: 3.0.3
166
+ rubygems_version: 3.2.16
167
167
  signing_key:
168
168
  specification_version: 4
169
169
  summary: HTTP Caching for Rack