Skip to content
Squeegy edited this page Sep 12, 2010 · 3 revisions

Running Fleximage without page caching on your production site is A VERY BAD IDEA. Given a decent amount of traffic, you server will melt. Dynamic image generation is a computationally expensive task. Depending on the complexity of your transformations it could take up to a few seconds for the image to be rendered. The only way to make this a viable option is page caching.

The good news is that page caching Fleximage output is no different from the standard sort of caching that Rails already uses for HTML. I would greatly suggest reading up on the official page caching documentation

To get this working with our photo example, we would add this to our app/controllers/photos_controller.rb:

class PhotosController < ApplicationController
  caches_page :show

  ...

  def update
    # ... standard update code
    expire_photo(@photo)
  end

  def destroy
    # ... standard destroy code
    expire_photo(@photo)
  end

  private
    def expire_photo(photo)
      expire_page formatted_photo_path(photo, :jpg)
    end
end

Now, when you run in production mode, your generated image will get saved to the public directly and henceforth served wicked fast by your webserver (like Apache) without even invoking the Rails app at all. Given that most images on websites are viewed far more times than they are updated, this keeps performance of your dynamic image wicked fast.

There are many other ways to expire your caches when things change, and I suggest you get familiar with the general Rails caching goodness to get the most out of it for the least headache. Sadly, dealing with caching, and making sure everything expires when it should in a larger application, can be quite tedious and error prone. Stick with it, you’ll get it.

Clone this wiki locally