Per session caching options?


#1

I want to cache the sidebar(which is a partial) which is unique to a
particular user. (like the right sidebar of basecamp ).

  1. Can fragment caching achieve this ? How can you clear the cache
    when the session is cleared ? And what makes better sense ? Storing it
    in DB/memory/file. For a full page cache
    it makes better sense to use the filesystem since the webserver can
    serve it without hitting the rails framework. But in this case since
    it is a partial being cached rails need to read it from the filesystem
    to produce the final HTML. So is the filesystem read overhead worth it
    ? (Correct me if i’m wrong.)
  2. You can just stick the html produced first time in a session object
    (session[:sidebar] ) and then use it in the views. And then use a
    better session store (SQLsessionstore/memcached).

To me the 2nd option looks much better. Any reason not to use session
for this ?

Regards
Manu


#2

I would use fragment caching for this requirement, as this is exactly
what it is meant for.

You can change the fragment cache store, just like you can the session
store, so you can use memcache for the fragment cache and avoid hitting
the disk (although the disk is faster than the network if you put your
cache on a different machine)

When using the memcache store, you get the added benefit of not needing
to expire the fragment. The memcache is always a fixed size, so the
cache doesn’t grow out of control, and eventually your orphaned data
will be kicked out.


#3

Yeah, that is what I thought too. (using fragment caching). But I
could not find any way to caching per user. All the caching seems to
be per action.

And what is wrong with option 2? ie storing it in session ?


#4

Any time you update the session, the entire session will be written out
(to disk, or to the database). The overhead of having a large chunk of
data in there will cause performance problems if you update other
session info, including flash messages.

Fragment caching allows you to specify the key:

cache(:action => “list”, :action_suffix => “#{user_id}”) do

You can use user specific info in the action_suffix parameter.