Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

Request to add file caching to the Rails version

#2

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.

Reported by Michael Kastner · June 9th, 2008 @ 02:43 AM

State: new
Milestone: none
Assigned to: Bruce Williams Bruce Williams

Activity

  1. Michael Kastner
    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

    June 9th, 2008 @ 03:42 PM

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.