Skip to content

Loading…

add rack-cache.force-pass option #37

Merged
merged 2 commits into from

2 participants

@tekwiz

I have a need to optionally pass-through rack-cache. I have added the ability to set the rack-cache.force-pass env variable that can be set by a previous rack app, which forces rack-cache to pass through.

@rtomayko rtomayko merged commit 6943584 into rtomayko:master
@rtomayko
Owner

I'm kind of amazed there's not already some way to get that behavior. Can you tell me about the circumstances where you need this? I can think of a few cases but I'm curious as to what you ran into.

@tekwiz

Thanks for merging this in.

There are 2 use cases that we have. First is testing: allowing a "secret" HTTP X-header or query variable to activate the force-pass variable. The second is to pass on specific versions of our application, for example, a "slow mobile" version which uses no javascript and therefore can't be cached when authenticated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 9, 2011
  1. @tekwiz
Commits on Sep 10, 2011
  1. @tekwiz
Showing with 18 additions and 1 deletion.
  1. +7 −0 doc/faq.markdown
  2. +1 −1 lib/rack/cache/context.rb
  3. +10 −0 test/context_test.rb
View
7 doc/faq.markdown
@@ -110,6 +110,13 @@ frameworks are based entirely on manual purge and do not support validation at
the cache level.
+<a class='hash' id='force-pass' href='#force-pass'>#</a>
+
+### Q: How do I bypass rack-cache on a per-request basis?
+
+Set the `rack-cache.force-pass` variable in the rack environment to `true`.
+
+
<a class='hash' id='efficient-validation' href='#efficient-validation'>#</a>
### Q: What does "Efficient Validation" mean?
View
2 lib/rack/cache/context.rb
@@ -61,7 +61,7 @@ def call!(env)
response =
if @request.get? || @request.head?
- if !@env['HTTP_EXPECT']
+ if !@env['HTTP_EXPECT'] and !@env['rack-cache.force-pass']
lookup
else
pass
View
10 test/context_test.rb
@@ -15,6 +15,16 @@
response.headers.should.not.include 'Age'
end
+ it 'passes on rack-cache.force-pass' do
+ respond_with 200
+ get '/', {"rack-cache.force-pass" => true}
+
+ app.should.be.called
+ response.should.be.ok
+ cache.trace.should == [:pass]
+ response.headers.should.not.include 'Age'
+ end
+
%w[post put delete].each do |request_method|
it "invalidates on #{request_method} requests" do
respond_with 200
Something went wrong with that request. Please try again.