Skip to content
This repository
Browse code

Disable client reload and revalidate requests by default

This was causing all kinds of confusion since refreshing in
the browser always caused a cache miss. People assumed that
rack-cache wasn't working at all. The allow_reload and
allow_revalidate options now default to false. This breaks
with RFC 2616 but is the desired config in a majority of
gateway cache scenarios.
  • Loading branch information...
commit d2681527c53ef14ce7dd7835e596341fdf23b32c 1 parent 0956b10
Ryan Tomayko authored
7 CHANGES
@@ -4,6 +4,13 @@
4 4 memcache-client library. These are the default unless the memcached
5 5 library has already been required.
6 6
  7 + * The "allow_reload" and "allow_revalidate" options now default to
  8 + false instead of true. This means we break with RFC 2616 out of
  9 + the box but this is the expected configuration in a huge majority
  10 + of gateway cache scenarios. See the docs on configuration
  11 + options for more information on these options:
  12 + http://tomayko.com/src/rack-cache/configuration
  13 +
7 14 ## 0.4.0 / March 2009
8 15
9 16 * Ruby 1.9.1 / Rack 1.0 compatible.
30 doc/configuration.markdown
Source Rendered
@@ -16,8 +16,8 @@ __Environment__.
16 16 When the __Rack::Cache__ object is instantiated:
17 17
18 18 use Rack::Cache,
19   - :verbose => true,
20   - :metastore => 'memcached://localhost:11211/',
  19 + :verbose => true,
  20 + :metastore => 'memcached://localhost:11211/',
21 21 :entitystore => 'file:/var/cache/rack'
22 22
23 23 Using __Rack__'s __Environment__:
@@ -81,6 +81,32 @@ If any of these headers are present in the request, the response is considered
81 81 private and will not be cached _unless_ the response is explicitly marked public
82 82 (e.g., `Cache-Control: public`).
83 83
  84 +### `allow_reload`
  85 +
  86 +A boolean specifying whether reload requests sent by the client should be
  87 +honored by the cache. When this option is enabled (`rack-cache.allow_reload`
  88 +is `true`), requests that include a `Cache-Control: no-cache` header cause
  89 +the cache to discard anything it has stored for the request and ask that the
  90 +response be fully generated.
  91 +
  92 +Most browsers include a `Cache-Control: no-cache` header when the user performs
  93 +a "hard refresh" (e.g., holding `Shift` while clicking the "Refresh" button).
  94 +
  95 +*IMPORTANT: Enabling this option globally allows all clients to break your cache.*
  96 +
  97 +### `allow_revalidate`
  98 +
  99 +A boolean specifying whether revalidate requests sent by the client should be
  100 +honored by the cache. When this option is enabled (`rack-cache.allow_revalidate`
  101 +is `true`), requests that include a `Cache-Control: max-age=0` header cause the
  102 +cache to assume its copy of the response is stale, resulting in a conditional
  103 +GET / validation request to be sent to the server.
  104 +
  105 +Most browsers include a `Cache-Control: max-age=0` header when the user performs
  106 +a refresh (e.g., clicking the "Refresh" button).
  107 +
  108 +*IMPORTANT: Enabling this option globally allows all clients to break your cache.*
  109 +
84 110 ### `cache_key`
85 111
86 112 TODO: Document custom cache keys
4 lib/rack/cache/options.rb
@@ -133,8 +133,8 @@ def initialize_options(options={})
133 133 'rack-cache.entitystore' => 'heap:/',
134 134 'rack-cache.default_ttl' => 0,
135 135 'rack-cache.private_headers' => ['Authorization', 'Cookie'],
136   - 'rack-cache.allow_reload' => true,
137   - 'rack-cache.allow_revalidate' => true
  136 + 'rack-cache.allow_reload' => false,
  137 + 'rack-cache.allow_revalidate' => false
138 138 }
139 139 self.options = options
140 140 end
34 test/context_test.rb
@@ -124,7 +124,8 @@
124 124 response.headers.should.include 'Age'
125 125 end
126 126
127   - it 'reloads responses when cache hits but no-cache request directive present' do
  127 + it 'reloads responses when cache hits but no-cache request directive present ' +
  128 + 'when allow_reload is set true' do
128 129 count = 0
129 130 respond_with 200, 'Cache-Control' => 'max-age=10000' do |req,res|
130 131 count+= 1
@@ -141,14 +142,16 @@
141 142 response.body.should.equal 'Hello World'
142 143 cache.trace.should.include :fresh
143 144
144   - get '/', 'HTTP_CACHE_CONTROL' => 'no-cache'
  145 + get '/',
  146 + 'rack-cache.allow_reload' => true,
  147 + 'HTTP_CACHE_CONTROL' => 'no-cache'
145 148 response.should.be.ok
146 149 response.body.should.equal 'Goodbye World'
147 150 cache.trace.should.include :reload
148 151 cache.trace.should.include :store
149 152 end
150 153
151   - it 'does not reload responses when allow_reload is set false' do
  154 + it 'does not reload responses when allow_reload is set false (default)' do
152 155 count = 0
153 156 respond_with 200, 'Cache-Control' => 'max-age=10000' do |req,res|
154 157 count+= 1
@@ -171,9 +174,17 @@
171 174 response.should.be.ok
172 175 response.body.should.equal 'Hello World'
173 176 cache.trace.should.not.include :reload
  177 +
  178 + # test again without explicitly setting the allow_reload option to false
  179 + get '/',
  180 + 'HTTP_CACHE_CONTROL' => 'no-cache'
  181 + response.should.be.ok
  182 + response.body.should.equal 'Hello World'
  183 + cache.trace.should.not.include :reload
174 184 end
175 185
176   - it 'revalidates fresh cache entry when max-age request directive is exceeded' do
  186 + it 'revalidates fresh cache entry when max-age request directive is exceeded ' +
  187 + 'when allow_revalidate option is set true' do
177 188 count = 0
178 189 respond_with do |req,res|
179 190 count+= 1
@@ -192,7 +203,9 @@
192 203 response.body.should.equal 'Hello World'
193 204 cache.trace.should.include :fresh
194 205
195   - get '/', 'HTTP_CACHE_CONTROL' => 'max-age=0'
  206 + get '/',
  207 + 'rack-cache.allow_revalidate' => true,
  208 + 'HTTP_CACHE_CONTROL' => 'max-age=0'
196 209 response.should.be.ok
197 210 response.body.should.equal 'Goodbye World'
198 211 cache.trace.should.include :stale
@@ -200,7 +213,7 @@
200 213 cache.trace.should.include :store
201 214 end
202 215
203   - it 'does not revalidate fresh cache entry when enable_revalidate option is set false' do
  216 + it 'does not revalidate fresh cache entry when enable_revalidate option is set false (default)' do
204 217 count = 0
205 218 respond_with do |req,res|
206 219 count+= 1
@@ -227,6 +240,15 @@
227 240 cache.trace.should.not.include :stale
228 241 cache.trace.should.not.include :invalid
229 242 cache.trace.should.include :fresh
  243 +
  244 + # test again without explicitly setting the allow_revalidate option to false
  245 + get '/',
  246 + 'HTTP_CACHE_CONTROL' => 'max-age=0'
  247 + response.should.be.ok
  248 + response.body.should.equal 'Hello World'
  249 + cache.trace.should.not.include :stale
  250 + cache.trace.should.not.include :invalid
  251 + cache.trace.should.include :fresh
230 252 end
231 253 it 'fetches response from backend when cache misses' do
232 254 respond_with 200, 'Expires' => (Time.now + 5).httpdate

0 comments on commit d268152

Please sign in to comment.
Something went wrong with that request. Please try again.