Skip to content

Commit

Permalink
Merge aughr's entity store native ttl support
Browse files Browse the repository at this point in the history
See rtomayko#33 for some caveats with this approach.

Conflicts:
	test/entitystore_test.rb
  • Loading branch information
rtomayko committed Sep 18, 2011
2 parents 2c6fc2b + a0a3e92 commit 8c666cc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
16 changes: 8 additions & 8 deletions lib/rack/cache/entitystore.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion lib/rack/cache/metastore.rb
Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion lib/rack/cache/options.rb
Expand Up @@ -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] }
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/entitystore_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 8c666cc

Please sign in to comment.