# Per session caching options?

**URL:** <https://www.ruby-forum.com/t/per-session-caching-options/80613>\
**Category:** Rails\
**Created:** [December 29, 2006, 8:41am UTC](https://www.ruby-forum.com/t/per-session-caching-options/80613 "2006-12-29T08:41:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 29, 2006, 8:41am UTC](https://www.ruby-forum.com/t/per-session-caching-options/80613/1 "2006-12-29T08:41:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 29, 2006, 9:15pm UTC](https://www.ruby-forum.com/t/per-session-caching-options/80613/2 "2006-12-29T21:15:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 29, 2006, 11:42pm UTC](https://www.ruby-forum.com/t/per-session-caching-options/80613/3 "2006-12-29T23:42:14Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 30, 2006, 1:26am UTC](https://www.ruby-forum.com/t/per-session-caching-options/80613/4 "2006-12-30T01:26:30Z")

</div>

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.
