Last active
October 14, 2015 20:31
-
-
Save seanhandley/340b647f4b28c6d5e6f2 to your computer and use it in GitHub Desktop.
Cache fetch
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
# This is a handy pattern for handling slow computations like API calls | |
def slow_results | |
Rails.cache.fetch("slow_api_results", expires_in: 10.minutes) do | |
slow_api_call | |
end | |
end | |
slow_results # Takes a few seconds to load first time | |
# => "Lorem ipsum..." | |
slow_results # Loads from cache (and so very quickly) second time | |
# => "Lorem ipsum..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment