New cache system using proxy_cache_bypass

Instead of using ngx_cache_purge which was buggy, we are trying
proxy_cache_bypass, which is internal to nginx. This is A Good Thing
(assuming it works), because when we call HEAD on the purge item, it
also warms the cache for us, which the previous system did not do. We
want newly updated files to cache warm, because they are expected to be
used quickly.
This commit is contained in:
Kyle Drake 2015-07-27 15:44:34 -07:00
parent 0f0baa78ff
commit 7bd2ad6b51
2 changed files with 17 additions and 16 deletions

View file

@ -6,8 +6,8 @@ describe PurgeCacheWorker do
end end
it 'throws exception without 200 or 404 http status' do it 'throws exception without 200 or 404 http status' do
stub_request(:get, "http://#{@test_ip}/:cache/purge/test.jpg"). stub_request(:head, "http://#{@test_ip}/test.jpg").
with(headers: {'Host' => 'kyledrake.neocities.org'}) with(headers: {'Host' => 'kyledrake.neocities.org', 'Cache-Purge' => '1'})
.to_return(status: 503) .to_return(status: 503)
worker = PurgeCacheWorker.new worker = PurgeCacheWorker.new
@ -18,8 +18,8 @@ describe PurgeCacheWorker do
end end
it 'handles 404 without exception' do it 'handles 404 without exception' do
stub_request(:get, "http://#{@test_ip}/:cache/purge/test.jpg"). stub_request(:head, "http://#{@test_ip}/test.jpg").
with(headers: {'Host' => 'kyledrake.neocities.org'}) with(headers: {'Host' => 'kyledrake.neocities.org', 'Cache-Purge' => '1'})
.to_return(status: 404) .to_return(status: 404)
worker = PurgeCacheWorker.new worker = PurgeCacheWorker.new
@ -27,35 +27,35 @@ describe PurgeCacheWorker do
end end
it 'sends a purge request' do it 'sends a purge request' do
stub_request(:get, "http://#{@test_ip}/:cache/purge/test.jpg"). stub_request(:head, "http://#{@test_ip}/test.jpg").
with(headers: {'Host' => 'kyledrake.neocities.org'}) with(headers: {'Host' => 'kyledrake.neocities.org', 'Cache-Purge' => '1'})
.to_return(status: 200) .to_return(status: 200)
worker = PurgeCacheWorker.new worker = PurgeCacheWorker.new
worker.perform @test_ip, 'kyledrake', '/test.jpg' worker.perform @test_ip, 'kyledrake', '/test.jpg'
assert_requested :get, "http://#{@test_ip}/:cache/purge/test.jpg" assert_requested :head, "http://#{@test_ip}/test.jpg"
end end
it 'handles spaces correctly' do it 'handles spaces correctly' do
stub_request(:get, "http://#{@test_ip}/:cache/purge/te st.jpg"). stub_request(:head, "http://#{@test_ip}/te st.jpg").
with(headers: {'Host' => 'kyledrake.neocities.org'}) with(headers: {'Host' => 'kyledrake.neocities.org', 'Cache-Purge' => '1'})
.to_return(status: 200) .to_return(status: 200)
url = Addressable::URI.encode_component( url = Addressable::URI.encode_component(
"http://#{@test_ip}/:cache/purge/te st.jpg", "http://#{@test_ip}/te st.jpg",
Addressable::URI::CharacterClasses::QUERY Addressable::URI::CharacterClasses::QUERY
) )
worker = PurgeCacheWorker.new worker = PurgeCacheWorker.new
worker.perform @test_ip, 'kyledrake', '/te st.jpg' worker.perform @test_ip, 'kyledrake', '/te st.jpg'
assert_requested :get, url assert_requested :head, url
end end
it 'works without forward slash' do it 'works without forward slash' do
stub_request(:get, "http://#{@test_ip}/:cache/purge/test.jpg"). stub_request(:head, "http://#{@test_ip}/test.jpg").
with(headers: {'Host' => 'kyledrake.neocities.org'}) with(headers: {'Host' => 'kyledrake.neocities.org', 'Cache-Purge' => '1'})
.to_return(status: 200) .to_return(status: 200)
worker = PurgeCacheWorker.new worker = PurgeCacheWorker.new

View file

@ -15,12 +15,13 @@ class PurgeCacheWorker
path = '/' + path if path[0] != '/' path = '/' + path if path[0] != '/'
url = Addressable::URI.encode_component( url = Addressable::URI.encode_component(
"http://#{proxy_ip}/:cache/purge#{path}", "http://#{proxy_ip}#{path}",
Addressable::URI::CharacterClasses::QUERY Addressable::URI::CharacterClasses::QUERY
) )
begin begin
RestClient::Request.execute method: :get, url: url, timeout: HTTP_TIMEOUT, headers: { RestClient::Request.execute method: :head, url: url, timeout: HTTP_TIMEOUT, headers: {
host: URI::encode("#{username}.neocities.org") host: URI::encode("#{username}.neocities.org"),
cache_purge: '1'
} }
rescue RestClient::ResourceNotFound rescue RestClient::ResourceNotFound
end end