dfens dfens - 8 months ago 70
Ruby Question

Conditional Page Caching [Solution: Conditional Fragment Caching]

Suppose that I have controller

with action

I want to cache index page so I'm doing:

caches_page :index

but want it to cache only for users that are not signed in. If I'll make conditional like:

caches_page :index, :if => :user_not_signed_in?

Page will be cached while first not logged in user comes. Now every logged in user also see not-logged in content. Is there a way to separate this action without changing url?


What you want couldn't be achieved;

A page is cached or is not cached. The process checks the existence of a html file or process it.

Still you have two alternatives:

  • use action caching or fragment caching (not recommended)

  • more recommended: load user specific part with ajax so you'll only have one page always cached and specific data inserted dynamically (like stackoverflow)

Here are state of the art explanations: http://railslab.newrelic.com/scaling-rails