class Rack::PageCaching::Cache

Public Class Methods

delete(path) click to toggle source
# File lib/rack/page_caching/cache.rb, line 50
def self.delete(path)
  expand_path('expire_page', path) do |full_path|
    Dir[full_path].each do |file|
      ::File.delete(file)
      ::File.delete(file + '.gz') if ::File.exist?(file + '.gz')
    end
  end
end
new(response) click to toggle source
# File lib/rack/page_caching/cache.rb, line 7
def initialize(response)
  @response = response
end
store(response) click to toggle source
# File lib/rack/page_caching/cache.rb, line 32
def self.store(response)
  Rack::PageCaching::Cache.new(response).store
end
write_file(content, path, gzip_level) click to toggle source
# File lib/rack/page_caching/cache.rb, line 36
def self.write_file(content, path, gzip_level)
  expand_path('write_page', path) do |full_path|
    FileUtils.makedirs(::File.dirname(full_path))
    ::File.open(full_path, 'wb+') { |f| content.each { |c| f.write(c) } }
    if gzip_level
      Zlib::GzipWriter.open(full_path + '.gz', gzip_level) do |f|
        content.each do |c|
          f.write(c)
        end
      end
    end
  end
end

Private Class Methods

expand_path(name, path) { |full_path| ... } click to toggle source
# File lib/rack/page_caching/cache.rb, line 61
def self.expand_path(name, path)
  env = Rack::PageCaching.environment
  full_path = ::File.join(env.page_cache_directory, path)
  env.instrument name, full_path do
    yield full_path
  end
end

Public Instance Methods

gzip_level() click to toggle source
# File lib/rack/page_caching/cache.rb, line 28
def gzip_level
  @response.gzip_level || config.page_cache_compression
end
page_cache_path() click to toggle source
# File lib/rack/page_caching/cache.rb, line 17
def page_cache_path
  path = Rack::Utils.unescape(@response.path.chomp('/'))
  if !path.include?('..')
    type = Rack::PageCaching::MimeTypes.extension_for @response.content_type
    path = '/index' if path.empty?
    path = "#{path}#{type}" unless /#{Regexp.quote(type)}\z/.match(path)
    path = "/#{@response.host}#{path}"  if config.include_hostname?
    path
  end
end
store() click to toggle source
# File lib/rack/page_caching/cache.rb, line 11
def store
  if path = page_cache_path
    self.class.write_file(@response.body, path, gzip_level)
  end
end

Private Instance Methods

config() click to toggle source
# File lib/rack/page_caching/cache.rb, line 69
def config
  Rack::PageCaching.environment
end