Skip to content

Instantly share code, notes, and snippets.

@pacoguzman
Forked from nickel/gist:126298
Created June 9, 2009 13:54
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 pacoguzman/126506 to your computer and use it in GitHub Desktop.
Save pacoguzman/126506 to your computer and use it in GitHub Desktop.
module SimpleCachingTTL
CACHETIME = 24 * 3600 # Once per day
def cache(text, path)
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w') { |f| f.write( text ) }
text
end
def set_and_get_cache(page, render = 'erb')
uri = request.env["REQUEST_URI"] == "/" ? 'index' : request.env["REQUEST_URI"]
path = File.join(File.dirname(__FILE__), 'cache', "#{uri}.html")
if File.exists?(path) && ttl?(path)
File.open(path)
else
cache(send(render, page), path)
end
end
def ttl?(path)
File.mtime(path) + CACHETIME > Time.now
end
end
Sinatra::Default.send :include, SimpleCachingTTL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment