Permalink
Browse files

Merge aughr's entity store native ttl support

See #33 for some caveats with this approach.

Conflicts:
	test/entitystore_test.rb
  • Loading branch information...
2 parents 2c6fc2b + a0a3e92 commit 8c666cc04b637df11577d19c0c025bc96e978d3d @rtomayko committed Sep 18, 2011
Showing with 30 additions and 10 deletions.
  1. +8 −8 lib/rack/cache/entitystore.rb
  2. +5 −1 lib/rack/cache/metastore.rb
  3. +8 −1 lib/rack/cache/options.rb
  4. +9 −0 test/entitystore_test.rb
@@ -58,7 +58,7 @@ def read(key)
end
# Write the Rack response body immediately and return the SHA1 key.
- def write(body)
+ def write(body, ttl=nil)
buf = []
key, size = slurp(body) { |part| buf << part }
@hash[key] = buf
@@ -119,7 +119,7 @@ def open(key)
nil
end
- def write(body)
+ def write(body, ttl=nil)
filename = ['buf', $$, Thread.current.object_id].join('-')
temp_file = storage_path(filename)
key, size =
@@ -227,10 +227,10 @@ def read(key)
data
end
- def write(body)
+ def write(body, ttl=nil)
buf = StringIO.new
key, size = slurp(body){|part| buf.write(part) }
- [key, size] if cache.set(key, buf.string)
+ [key, size] if cache.set(key, buf.string, ttl)
end
def purge(key)
@@ -267,10 +267,10 @@ def read(key)
nil
end
- def write(body)
+ def write(body, ttl=0)
buf = StringIO.new
key, size = slurp(body){|part| buf.write(part) }
- cache.set(key, buf.string, 0, false)
+ cache.set(key, buf.string, ttl, false)
[key, size]
end
@@ -315,10 +315,10 @@ def open(key)
end
end
- def write(body)
+ def write(body, ttl=nil)
buf = StringIO.new
key, size = slurp(body){|part| buf.write(part) }
- cache.put(key, buf.string)
+ cache.put(key, buf.string, ttl)
[key, size]
end
@@ -57,7 +57,11 @@ def store(request, response, entity_store)
# write the response body to the entity store if this is the
# original response.
if response.headers['X-Content-Digest'].nil?
- digest, size = entity_store.write(response.body)
+ if request.env['rack-cache.use_native_ttl'] && response.fresh?
+ digest, size = entity_store.write(response.body, response.ttl)
+ else
+ digest, size = entity_store.write(response.body)
+ end
response.headers['X-Content-Digest'] = digest
response.headers['Content-Length'] = size.to_s unless response.headers['Transfer-Encoding']
response.body = entity_store.open(digest)
@@ -8,6 +8,8 @@ module Rack::Cache
# below are stored in the Rack Environment as "rack-cache.<option>", where
# <option> is the option name.
module Options
+ extend self
+
def self.option_accessor(key)
name = option_name(key)
define_method(key) { || options[name] }
@@ -95,6 +97,10 @@ def option_name(key)
# default for compliance with RFC 2616.
option_accessor :allow_revalidate
+ # Specifies whether the underlying entity store's native expiration should
+ # be used.
+ option_accessor :use_native_ttl
+
# 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
@@ -134,7 +140,8 @@ def initialize_options(options={})
'rack-cache.default_ttl' => 0,
'rack-cache.private_headers' => ['Authorization', 'Cookie'],
'rack-cache.allow_reload' => false,
- 'rack-cache.allow_revalidate' => false
+ 'rack-cache.allow_revalidate' => false,
+ 'rack-cache.use_native_ttl' => false,
}
self.options = options
end
View
@@ -24,6 +24,15 @@ def sha_like?
data.should.equal 'My wild love went riding,'
end
+ it 'takes a ttl parameter for #write' do
+ key, size = @store.write(['My wild love went riding,'], 0)
+ key.should.not.be.nil
+ key.should.be.sha_like
+
+ data = @store.read(key)
+ data.should.equal 'My wild love went riding,'
+ end
+
it 'correctly determines whether cached body exists for key with #exist?' do
key, size = @store.write(['She rode to the devil,'])
@store.should.exist key

0 comments on commit 8c666cc

Please sign in to comment.