Created
July 8, 2016 01:06
-
-
Save sillypog/1545b327e3d35853d66c29d1051c8e2a to your computer and use it in GitHub Desktop.
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
class Cache | |
def self.fetch(table_name, fields, &block) | |
result = redis.get(table_name + ':' + fields.values.join('|')) | |
unless result | |
# block allows us to try to look for the value elsewhere, if requested | |
# NOTE: we are not currently passing a block, so this syntax is sufficiently fast | |
# However, when a block is present it is faster to use block_given? and yield | |
if block | |
result = block.call | |
end | |
unless result.nil? | |
redis.set(table_name + ':' + fields.values.join('|'), result) | |
end | |
end | |
!result || result.empty? ? nil : result | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment