Skip to content

Instantly share code, notes, and snippets.

@rafael
Created October 28, 2013 16:35
Show Gist options
  • Select an option

  • Save rafael/7200174 to your computer and use it in GitHub Desktop.

Select an option

Save rafael/7200174 to your computer and use it in GitHub Desktop.
Cache page based on actionpack_page_caching
require 'fileutils'
module Openair
module CachePage
# This methods come from action_pack/cache_page'
# https://github.com/rails/actionpack-page_caching
# We didn't need the whole gem. So I decided to get
# just what we need :)
def cache_page(content, path, extension = '.html', gzip = Zlib::BEST_COMPRESSION)
path = page_cache_path(path, extension)
FileUtils.makedirs(File.dirname(path))
File.open(path, 'wb+') { |f| f.write(content) }
if gzip
Zlib::GzipWriter.open(path + '.gz', gzip) { |f| f.write(content) }
end
end
private
def page_cache_path(path, extension = nil)
page_cache_directory = "#{Rails.root.to_s}/public"
page_cache_directory.to_s + '/' + path + extension
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment