Request to add file caching to the Rails version
-
Michael Kastner
This a short example of a hack for caching documents. What it basically does is that it checks whether a cached file already exists.
If a cached file exists the request is redirected to the url, where the file is located.
If no cached file exists, a regular render is kicked off. After the pdf is rendered the after_filter-method cache_pdf grabs the response stream and saves it to the caching location.
I think, that's pretty straight forward. Just an idea, how caching could work:
class PublicMagazinesController < PublicController
layout nil
after_filter :cache_pdf, :only => :publish
DIR_URL = '/cache/magazines/'
DIR_PATH = File.join('/public/', DIR_URL)
def publish
@magazine = Magazine.find(params[:id])
@file_path = File.join(RAILS_ROOT, DIR_PATH, @magazine.file_name)
if File.exist? @file_path
redirect_to File.join(DIR_URL, @magazine.file_name)
else
@cache_rtex_document = true
render :template => @magazine.template,
:filename => @magazine.file_name,
:preprocess => true
end
end
private
def cache_pdf
if @cache_rtex_document
path = File.join(RAILS_ROOT, DIR_PATH, @magazine.file_name)
FileUtils.makedirs(File.dirname(path))
File.open(path, "wb+") { |f|
response.body.call(response, f)
}
end
end
end
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.