Skip to content

Instantly share code, notes, and snippets.

@bennacer860
Last active July 6, 2017 02:09
Show Gist options
  • Save bennacer860/fac8af6d79315914aab5206ba135f547 to your computer and use it in GitHub Desktop.
Save bennacer860/fac8af6d79315914aab5206ba135f547 to your computer and use it in GitHub Desktop.
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