Permalink
Browse files

Gracefully degrade when cache store goes offline

The metastore reads back the cached body after storing it. If the cache
store is offline (e.g. memcached goes down) then the value read back
will be nil. This commit falls back to the original body when that
happens.
  • Loading branch information...
1 parent 06d089a commit fcb3fbbc28e997964ec2a44510141a27e50bbcb2 @alau alau committed May 29, 2012
Showing with 21 additions and 1 deletion.
  1. +1 −1 lib/rack/cache/metastore.rb
  2. +20 −0 test/metastore_test.rb
@@ -64,7 +64,7 @@ def store(request, response, entity_store)
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)
+ response.body = entity_store.open(digest) || response.body
end
# read existing cache entries, remove non-varying, and add this one to
View
@@ -182,6 +182,26 @@ def self.call(request); request.path_info.reverse end
@store.lookup(@request, @entity_store).should.be.nil
end
+ it 'gracefully degrades if the cache store stops working' do
+ @store = Class.new(Rack::Cache::MetaStore) do
+ def purge(*args); nil end
+ def read(*args); [] end
+ def write(*args); nil end
+ end.new
+ @entity_store = Class.new(Rack::Cache::EntityStore) do
+ def exists?(*args); false end
+ def open(*args); nil end
+ def read(*args); nil end
+ def write(*args); nil end
+ def purge(*args); nil end
+ end.new
+
+ request = mock_request('/test', {})
+ response = mock_response(200, {}, ['test'])
+ @store.store(request, response, @entity_store)
+ response.body.should == ['test']
+ end
+
# Vary =======================================================================
it 'does not return entries that Vary with #lookup' do

0 comments on commit fcb3fbb

Please sign in to comment.