Created
July 7, 2011 19:11
-
-
Save elight/1070295 to your computer and use it in GitHub Desktop.
Cache conditionally or just yield the block uncached
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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