Created
September 7, 2015 13:59
-
-
Save rob-murray/43a2aa27eaefe39600fb to your computer and use it in GitHub Desktop.
Ruby cache hashe
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
# a method that is a simple hash acting as a cache to remote service, db call, etc | |
# | |
def a_cache | |
@a_cache ||= Hash.new do |cache, reference| | |
cache[reference] = Service.fetch(reference) | |
end | |
end | |
# usage | |
# a_cache[reference] | |
> a_cache["abc012012"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment