Created
June 4, 2009 21:50
-
-
Save xhochy/123872 to your computer and use it in GitHub Desktop.
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
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