Skip to content

Instantly share code, notes, and snippets.

@xhochy
Created June 4, 2009 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xhochy/123872 to your computer and use it in GitHub Desktop.
Save xhochy/123872 to your computer and use it in GitHub Desktop.
module Cache
def Cache.file(filename, cache_lifetime=604800)
if File.exists?(filename)
if File.mtime(filename) < (Time.now - cache_lifetime)
body = yield
File.open(filename, 'w') do |file|
file << body.to_yaml
end
return body
else
return YAML::load(File.open(filename, 'r').read)
end
else
body = yield
File.open(filename, 'w') do |file|
file << body.to_yaml
end
return body
end
end
end
# Example usage
# =============
Test = Cache.file('test.yml') do
Blood::get_dns_of(me)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment