Created
January 26, 2009 17:15
-
-
Save bjhess/52868 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
require 'lib/caching' | |
get '/' do | |
cache(erb(:index, :layout => :main)) | |
end |
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
#! /usr/bin/env ruby | |
# | |
# This script is to be run via cron while with the cwd in the | |
# Sinatra root directory. | |
# | |
require 'fileutils' | |
TWENTY_FOUR_HOURS = 60*60*24 | |
Dir['public/*.html'].each do |path| | |
mtime = File.mtime(path) | |
if (mtime.to_i + TWENTY_FOUR_HOURS) < Time.now.to_i | |
FileUtils.remove_file(path) | |
end | |
end | |
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
require 'fileutils' | |
helpers do | |
def cache(text) | |
# requests to / should be cached as index.html | |
uri = request.env["REQUEST_URI"] == "/" ? 'index' : request.env["REQUEST_URI"] | |
# Don't cache pages with query strings. | |
unless uri =~ /\?/ | |
uri << '.html' | |
# put all cached files in public/ | |
path = File.join(File.dirname(__FILE__), '..', 'public', uri) | |
# Write the text passed to the path | |
File.open(path, 'w') { |f| f.write( text ) } | |
end | |
return text | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment