module Sinatra::FragmentCache
Attributes
cache_path[RW]
options[RW]
path[RW]
Public Class Methods
registered(app)
click to toggle source
# File lib/sinatra_fragment_cache/fragment_cache.rb, line 43 def self.registered(app) app.extend Sinatra app.helpers FragmentCache app.set :fragment_cache_enabled, true app.set :fragment_cache_output_dir, lambda { "#{ app.root }/tmp/cache" } end
Public Instance Methods
fragment_cache(path, options = {}, &block)
click to toggle source
# File lib/sinatra_fragment_cache/fragment_cache.rb, line 5 def fragment_cache(path, options = {}, &block) @path = path @options = options file_name = options[:file_name] || 'index.cache' @cache_path = "#{ settings.fragment_cache_output_dir }/#{ path }/#{ file_name }" return block.call unless settings.fragment_cache_enabled @_out_buf = [] if cache = read_fragment @_out_buf << cache else pos = @_out_buf.length tmp = block.call write_fragment tmp[pos..-1] end end
read_fragment()
click to toggle source
# File lib/sinatra_fragment_cache/fragment_cache.rb, line 23 def read_fragment now = Time.now if File.file?(cache_path) if options[:expires_in] (current_age = (now - File.mtime(cache_path)).to_i / 60) return false if (current_age > options[:expires_in]) end return File.read(cache_path) end false end
write_fragment(buffer)
click to toggle source
# File lib/sinatra_fragment_cache/fragment_cache.rb, line 35 def write_fragment(buffer) FileUtils.mkdir_p "#{ settings.fragment_cache_output_dir }/#{ path }" file = File.new(cache_path, 'w+') file.write(buffer) file.close buffer end