Skip to content

Commit

Permalink
Purge cache_key if response headers do not include content digest (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
evman182 authored Feb 28, 2024
1 parent ebc5fa1 commit 4344620
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/rack/cache/meta_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def lookup(request, entity_store)
return nil if match.nil?

_, res = match
if body = entity_store.open(res['x-content-digest'])
entity_key = res['x-content-digest']
if entity_key && body = entity_store.open(entity_key)
restore_response(res, body)
else
# the metastore referenced an entity that doesn't exist in
Expand Down
13 changes: 13 additions & 0 deletions test/meta_store_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,19 @@ def self.call(request); request.path_info.reverse end
mock.verify
end

it 'purges meta store entry when the entry does not contain the digest header' do
cache_key = store_simple_entry
meta_entry = @store.read(cache_key)
meta_entry.grep(Array).flatten.each { |h| h.is_a?(Hash) && h.delete('x-content-digest') }
@store.write(cache_key, meta_entry)
mock = Minitest::Mock.new
mock.expect :call, nil, [@store.cache_key(@request)]
@store.stub(:purge, mock) do
@store.lookup(@request, nil)
end
mock.verify
end

it 'warns once if purge is not implemented' do
store_simple_entry
assert @response.headers['x-content-digest']
Expand Down

0 comments on commit 4344620

Please sign in to comment.