Permalink
Browse files

allow_revalidate option; set false to ignore max-age directives in re…

…quests
  • Loading branch information...
1 parent 481a0f0 commit 129d982978e95c5f10e41b0185af321c7d781aea @rtomayko rtomayko committed Mar 14, 2009
Showing with 45 additions and 11 deletions.
  1. +2 −1 CHANGES
  2. +1 −1 lib/rack/cache/context.rb
  3. +14 −9 lib/rack/cache/options.rb
  4. +28 −0 test/context_test.rb
View
@@ -9,7 +9,8 @@
* Implement "Cache-Control: max-age=N" request directive by forcing
validation when the max-age provided exceeds the age of the cache
- entry.
+ entry. This can be disabled by setting the "allow_revalidate" option to
+ false.
* Properly implement "Cache-Control: no-cache" request directive by
performing a full reload. RFC 2616 states that when "no-cache" is
@@ -110,7 +110,7 @@ def not_modified?(response)
# Whether the cache entry is "fresh enough" to satisfy the request.
def fresh_enough?(entry)
if entry.fresh?
- if max_age = @request.cache_control.max_age
+ if allow_revalidate? && max_age = @request.cache_control.max_age
max_age > 0 && max_age >= entry.age
else
true
View
@@ -85,12 +85,16 @@ 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
+ # Specifies whether the client can force a cache revalidate by including
+ # a Cache-Control "max-age=0" directive in the request. This is enabled by
+ # default for compliance with RFC 2616.
+ option_accessor :allow_revalidate
+
# 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,14 +126,15 @@ def set(option, value=self, &block)
private
def initialize_options(options={})
@default_options = {
- 'rack-cache.cache_key' => Key,
- 'rack-cache.verbose' => true,
- 'rack-cache.storage' => Rack::Cache::Storage.instance,
- 'rack-cache.metastore' => 'heap:/',
- 'rack-cache.entitystore' => 'heap:/',
- 'rack-cache.default_ttl' => 0,
- 'rack-cache.private_headers' => ['Authorization', 'Cookie'],
- 'rack-cache.allow_reload' => true
+ 'rack-cache.cache_key' => Key,
+ 'rack-cache.verbose' => true,
+ 'rack-cache.storage' => Rack::Cache::Storage.instance,
+ 'rack-cache.metastore' => 'heap:/',
+ 'rack-cache.entitystore' => 'heap:/',
+ 'rack-cache.default_ttl' => 0,
+ 'rack-cache.private_headers' => ['Authorization', 'Cookie'],
+ 'rack-cache.allow_reload' => true,
+ 'rack-cache.allow_revalidate' => true
}
self.options = options
end
View
@@ -200,6 +200,34 @@
cache.trace.should.include :store
end
+ it 'does not revalidate fresh cache entry when enable_revalidate option is set false' do
+ count = 0
+ respond_with do |req,res|
+ count+= 1
+ res['Cache-Control'] = 'max-age=10000'
+ res['ETag'] = count.to_s
+ 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_revalidate' => false,
+ 'HTTP_CACHE_CONTROL' => 'max-age=0'
+ response.should.be.ok
+ response.body.should.equal 'Hello World'
+ cache.trace.should.not.include :stale
+ cache.trace.should.not.include :invalid
+ cache.trace.should.include :fresh
+ end
it 'fetches response from backend when cache misses' do
respond_with 200, 'Expires' => (Time.now + 5).httpdate
get '/'

0 comments on commit 129d982

Please sign in to comment.