Skip to content

Instantly share code, notes, and snippets.

@xaviershay
Created May 5, 2010 05:58
Show Gist options
  • Select an option

  • Save xaviershay/390430 to your computer and use it in GitHub Desktop.

Select an option

Save xaviershay/390430 to your computer and use it in GitHub Desktop.
# # Don't waste time testing your scripts!
# file_cache('results') { expensive_computation }
# file_cache('data') { data_from_the_internet }
def file_cache(key)
key = key.gsub(/\W/, '-')
base = File.dirname(__FILE__) + "/../../tmp/cache"
FileUtils.mkdir_p(base)
path = "#{base}/#{key}"
if File.exists?(path)
result = File.open(path) {|f| Marshal.load(f) }
else
result = yield
File.open(path, "w") {|f| Marshal.dump(result, f) }
end
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment