Skip to content

Instantly share code, notes, and snippets.

@elight
Created July 7, 2011 19:11
Show Gist options
  • Save elight/1070295 to your computer and use it in GitHub Desktop.
Save elight/1070295 to your computer and use it in GitHub Desktop.
Cache conditionally or just yield the block uncached
def conditionally_cache(key, args = {}, &block)
positive, negative = args[:if], args[:unless]
fail "Must supply at most one of :if or :unless" if positive && negative
if (positive && positive.call(key)) || (negative && !negative.call(key))
Rails.logger.debug "BENCH: cache hit, #{key.cache_key}"
cache(key, &block)
else
Rails.logger.debug "BENCH: NOT cache hit, #{key.cache_key}"
yield
end
""
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment