Skip to content

Instantly share code, notes, and snippets.

@seanhandley
Last active October 14, 2015 20:31
Show Gist options
  • Save seanhandley/340b647f4b28c6d5e6f2 to your computer and use it in GitHub Desktop.
Save seanhandley/340b647f4b28c6d5e6f2 to your computer and use it in GitHub Desktop.
Cache fetch
# 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