Request to add file caching to the Rails version
Reported by Michael Kastner | June 9th, 2008 @ 02:43 AM
When data changes do not occur frequently, it might be very useful to cache/store the generated documents as static files on the server's file system.
The cached files then can be served by the front-end web server and don't need to be generated all over again as long as the document's data or the template doesn't change.
Comments and changes to this ticket
-
Michael Kastner June 9th, 2008 @ 03:42 PM
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.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป
RTeX is a Ruby library (and web framework plugin) used to generate PDF documents on-the-fly using the LaTeX typesetting system.