Skip to content

Commit

Permalink
Caching mostly done
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Sadauskas committed May 22, 2008
1 parent c648a8e commit 5fa8ab8
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/resourceful/cache_manager.rb
Expand Up @@ -75,7 +75,7 @@ def lookup(request)
end

def store(request, response)
return unless response.cacheable?
return unless response.cachable?

entry = CacheEntry.new(request.request_time,
select_request_headers(request, response),
Expand Down
6 changes: 6 additions & 0 deletions lib/resourceful/response.rb
Expand Up @@ -35,6 +35,12 @@ def expired?
end
alias stale? expired?

def cachable?
return false if header['Vary'] == ['*']

true
end

# Algorithm taken from RCF2616#13.2.3
def current_age
age_value = Time.httpdate(header['Age'].first) if header['Age']
Expand Down
12 changes: 10 additions & 2 deletions spec/acceptance_spec.rb
Expand Up @@ -136,12 +136,20 @@
resp2 = resource.get
resp2.authoritative?.should be_false

resp2.object_id.should == resp.object_id
resp2.should == resp
end

it 'should store a fetched representation'

it 'should not store the representation if the server says not to'
it 'should not store the representation if the server says not to' do
resource = @accessor.resource('http://localhost:3000/header?{Vary:%20*}')
resp = resource.get
resp.authoritative?.should be_true
resp.should_not be_cachable

resp2 = resource.get
resp2.should_not == resp
end

it 'should use the cached version of the representation if it has not expired'

Expand Down
91 changes: 79 additions & 12 deletions spec/resourceful/cache_manager_spec.rb
Expand Up @@ -19,6 +19,36 @@
it 'should have a store method' do
@cm.should respond_to(:store)
end

describe '#select_request_headers' do
before do
@req_header = mock('header', :[] => nil)
@request = mock('request', :header => @req_header)

@resp_header = mock('header', :[] => nil)
@response = mock('response', :header => @resp_header)
end

it 'should select the request headers from the Vary header' do
@resp_header.should_receive(:[]).with('Vary')
@cm.select_request_headers(@request, @response)
end

it 'should pull the values from the request that match keys in the vary header' do
@resp_header.should_receive(:[]).with('Vary').twice.and_return(['foo', 'bar'])
@req_header.should_receive(:[]).with('foo').and_return('oof')
@req_header.should_receive(:[]).with('bar').and_return('rab')

header = @cm.select_request_headers(@request, @response)
header['foo'].should == 'oof'
header['bar'].should == 'rab'
end

it 'should return a new Header object' do
@cm.select_request_headers(@request, @response).should be_kind_of(Resourceful::Header)
end
end

end

describe Resourceful::NullCacheManager do
Expand All @@ -41,28 +71,65 @@

describe Resourceful::InMemoryCacheManager do
before do
@request = mock('request', :resource => mock('resource', :uri => 'uri'))
@response = mock('response', :authoritative= => nil, :cachable => true)
@request = mock('request', :resource => mock('resource', :uri => 'uri'),
:request_time => Time.utc(2008,5,22,15,00))
@response = mock('response', :header => {})

@entry = mock('cache entry', :response => @response)
@entry = mock('cache entry', :response => @response, :valid_for? => true)
Resourceful::InMemoryCacheManager::CacheEntry.stub!(:new).and_return(@entry)

@imcm = Resourceful::InMemoryCacheManager.new
@imcm.instance_variable_set("@collection", {'uri' => {@request => @entry}})
end

describe 'finding' do
before do
@response.stub!(:authoritative=)
@imcm.instance_variable_set("@collection", {'uri' => {@request => @entry}})
end

it 'should lookup the response by request' do
@imcm.lookup(@request).should == @response
end

it 'should lookup the response by request' do
@imcm.lookup(@request).should == @response
it 'should set the response to non-authoritative' do
@response.should_receive(:authoritative=).with(false)
@imcm.lookup(@request)
end
end

it 'should set the response to non-authoritative' do
@response.should_receive(:authoritative=).with(false)
@imcm.lookup(@request)
end
describe 'saving' do
before do
@response.stub!(:cachable?).and_return(true)
end

it 'should make a new cache entry' do
Resourceful::InMemoryCacheManager::CacheEntry.should_receive(:new).with(
Time.utc(2008,5,22,15,00),
{},
@response
)

it 'should store the response by request'
@imcm.store(@request, @response)
end

it 'should not store an entry if the response is not cachable'
it 'should store the response entity by request' do
@imcm.store(@request, @response)
col = @imcm.instance_variable_get("@collection")
col['uri'][@request].response.should == @response
end

it 'should check if the response is cachable' do
@response.should_receive(:cachable?).and_return(true)
@imcm.store(@request, @response)
end

it 'should not store an entry if the response is not cachable' do
@response.should_receive(:cachable?).and_return(false)
@imcm.store(@request, @response)
col = @imcm.instance_variable_get("@collection")
col['uri'][@request].should be_nil
end
end

end

Expand Down
19 changes: 19 additions & 0 deletions spec/resourceful/response_spec.rb
Expand Up @@ -98,6 +98,25 @@
@response.current_age.should == (2 * 60 * 60 + 2)
end

it 'should know if its #cachable?' do
@response.should respond_to(:cachable?)
end

it 'should normally be cachable' do
@response.cachable?.should be_true
end

def response_with_header(header = {})
Resourceful::Response.new(200, header, "")
end

it 'should not be cachable if the vary header is "*"' do
r = response_with_header('Vary' => ['*'])
r.cachable?.should be_false
end



end

end
Expand Down

0 comments on commit 5fa8ab8

Please sign in to comment.