Last active
July 6, 2017 02:09
-
-
Save bennacer860/fac8af6d79315914aab5206ba135f547 to your computer and use it in GitHub Desktop.
This file contains 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 | |
@redis = {} | |
def self.fetch key, &block | |
if @redis.key?(key) | |
# fetch and return result | |
puts "fetch from cache" | |
@redis[key] | |
else | |
if block_given? | |
# make the DB query and create a new entry for the request result | |
puts "did not find key in cache, executing block ..." | |
@redis[key] = yield(block) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment