Skip to content

Instantly share code, notes, and snippets.

@matthutchinson
Created May 24, 2013 16:20
Show Gist options
  • Save matthutchinson/5644640 to your computer and use it in GitHub Desktop.
Save matthutchinson/5644640 to your computer and use it in GitHub Desktop.
# WARNING: fragment cache is no longer cleared on deploy to clear completely
# use a version key or bump FRAGMENT_CACHE_VERSION above
def fragment_cache(name, options = {}, &block)
old_within_fragment = @within_fragment
@within_fragment = true
# trace execution for NewRelic
CachingHelper.trace_execution_scoped(["Custom/Fragment cache overhead: #{name}"]) do
if_conditions = Array.wrap(options[:if])
unless_conditions = Array.wrap(options[:unless])
# if name =~ /landingpage\/property/
# require 'pry'
# binding.pry
# end
result = nil
authenticity_token = form_authenticity_token
with_overriden_authenticity_token(placeholder_for('AUTHENTICITY_TOKEN', authenticity_token)) do
if if_conditions.all? and not unless_conditions.any? and not params[:ab_test]
cache(cache_key(name, options), :expires_in => options[:expires_in]) do
# CachingHelper.trace_execution_scoped(["Custom/Fragment cache render (cache miss): #{name}"], &block)
result = capture_as_erb(&block)
end
else
result = capture_as_erb(&block)
# CachingHelper.trace_execution_scoped(["Custom/Fragment cache render (cache skipped): #{name}"], &block)
end
end
concat(replace_placeholders(result))
end
ensure
@within_fragment = old_within_fragment
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment