require 'fileutils' module Nitro # Adds support for caching. module Caching # The Output caching subsystem stores whole pages in the # filesystem to be served directly be the front web server # (Lighttpd, Apache, etc) for optimal performance. # # Nitro promotes coding your application in such a way as to # allow for output caching to the greatest extend. Output # caching *is your friend*. #-- # gmosx, FIXME: Don't create excessive directories, use better # rewrite rules to handle xxx.html files. #++ module Output def self.included(base) # :nodoc: base.extend(ClassMethods) base.module_eval do cattr_accessor :output_cache_root, 'public' end end module ClassMethods def do_cache_output(path, content) filepath = output_cache_path(path) FileUtils.makedirs(File.dirname(filepath)) File.open(filepath, 'w+') { |f| f.write(content) } Logger.debug "Cached page '#{filepath}'" if $DBG end # Enable output caching for the given actions. def cache_output(*actions) return unless caching_enabled? str = actions.collect { |a| ":#{a}" }.join(', ') module_eval %{ after "do_cache_output", :on => [ #{str} ] } end private def output_cache_path(path) filename = ((path.empty? || path == '/') ? 'index.html' : path.dup) # filename.gsub!(/\/$/, '') filename << '/index.html' unless (filename.split('/').last || filename).include? '.' return output_cache_root + '/' + filename end end private def do_cache_output if caching_enabled? and caching_allowed? self.class.do_cache_output(@request.uri, @out) end end # Explicitly expire the output cached under the given # cache key. The cache key is typically the name of the # top level action responsible for generating the page. #-- # If you change this method, don't forget the CacheSweeper # expire method. #++ def expire_output(name) begin Logger.debug "Expirinig cache file '#{Server.public_root}/#{name}'" if $DBG FileUtils.rm_rf("#{Server.public_root}/#{name}") rescue Object => ex # gmosx: is this the right thing to do? end end alias_method :delete_output, :expire_output # Is caching allowed for this action (page)? The default # implementation does not cache post request or request # with query parameters. You can work arround the second # 'limitation' by cleverly using Nitro's implicit support # for 'nice' urls. def caching_allowed? not (@request.post? or @request.uri =~ /\?/) end end end end # * George Moschovitis