Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow_reload option; set false to ignore no-cache directives in requests

  • Loading branch information...
commit 481a0f0149fc0be3cf9758bb8b565b6d3a37c477 1 parent 7b82c11
@rtomayko rtomayko authored
View
3  CHANGES
@@ -19,7 +19,8 @@
first validate its entry with the origin. Previously, we implemented
"no-cache" on requests by passing so no new cache entry would be stored
based on the response. Now we treat it as a forced miss and enter the
- response into the cache if it's cacheable.
+ response into the cache if it's cacheable. This can be disabled by
+ setting the "allow_reload" option to false.
* Assume identical semantics for the "Pragma: no-cache" request header
as the "Cache-Control: no-cache" directive described above.
View
2  lib/rack/cache/context.rb
@@ -144,7 +144,7 @@ def invalidate
# stale, attempt to #validate the entry with the backend using conditional
# GET. When no matching cache entry is found, trigger #miss processing.
def lookup
- if @request.no_cache?
+ if @request.no_cache? && allow_reload?
record :reload
fetch
elsif entry = metastore.lookup(@request, entitystore)
View
9 lib/rack/cache/options.rb
@@ -85,6 +85,12 @@ def option_name(key)
# Default: ['Authorization', 'Cookie']
option_accessor :private_headers
+
+ # Specifies whether the client can force a cache reload by including a
+ # Cache-Control "no-cache" directive in the request. This is enabled by
+ # default for compliance with RFC 2616.
+ option_accessor :allow_reload
+
# The underlying options Hash. During initialization (or outside of a
# request), this is a default values Hash. During a request, this is the
# Rack environment Hash. The default values Hash is merged in underneath
@@ -122,7 +128,8 @@ def initialize_options(options={})
'rack-cache.metastore' => 'heap:/',
'rack-cache.entitystore' => 'heap:/',
'rack-cache.default_ttl' => 0,
- 'rack-cache.private_headers' => ['Authorization', 'Cookie']
+ 'rack-cache.private_headers' => ['Authorization', 'Cookie'],
+ 'rack-cache.allow_reload' => true
}
self.options = options
end
View
25 test/context_test.rb
@@ -148,6 +148,31 @@
cache.trace.should.include :store
end
+ it 'does not reload responses when allow_reload is set false' do
+ count = 0
+ respond_with 200, 'Cache-Control' => 'max-age=10000' do |req,res|
+ count+= 1
+ res.body = (count == 1) ? ['Hello World'] : ['Goodbye World']
+ end
+
+ get '/'
+ response.should.be.ok
+ response.body.should.equal 'Hello World'
+ cache.trace.should.include :store
+
+ get '/'
+ response.should.be.ok
+ response.body.should.equal 'Hello World'
+ cache.trace.should.include :fresh
+
+ get '/',
+ 'rack-cache.allow_reload' => false,
+ 'HTTP_CACHE_CONTROL' => 'no-cache'
+ response.should.be.ok
+ response.body.should.equal 'Hello World'
+ cache.trace.should.not.include :reload
+ end
+
it 'revalidates fresh cache entry when max-age request directive is exceeded' do
count = 0
respond_with do |req,res|
Please sign in to comment.
Something went wrong with that request. Please try again.