Skip to content

Instantly share code, notes, and snippets.

@rtomayko
Created September 8, 2008 07:48
Show Gist options
  • Save rtomayko/9395 to your computer and use it in GitHub Desktop.
Save rtomayko/9395 to your computer and use it in GitHub Desktop.
def defer(&block)
def block.each ; yield call ; end
block
end
app =
lambda do |env|
status = 200
headers = { 'Last-Modified' => exec_sql('SELECT MAX(updated_at) FROM foos').httpdate }
body = defer {
rows = exec_sql('SELECT * FROM foos')
# more heavy processing
render_template :foos => rows
}
[ status, headers, body ]
end
use Rack::ConditionalGet
run app
# Right? When the conditional GET hits, the defer block is never executed. If
# you can generate your validators quickly enough, you can get fairly decent
# throughput without having to resort to huge max-age w/ cache busting or manual
# purge.
#
# It feels like a lot of folks are under the impression that conditional GET is
# only useful for saving bandwidth when it's actually more interesting in these
# resource reducing cases. Put a validating cache in front of an app like this
# and your backends never have to generate the same response twice!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment