Cache a sidebar in the layout

Hey

I have a sidebar on every page of my site which is part of my layout.
This sidebar contains a list of the last 10 updated threads in the
forum.

I have a ‘setup_sidebar’ action in the application controller and a
‘before_filter’ in every controller to populate the variables in the
sidebar.

My question is – how on Earth do I cache this sidebar??

Would I use fragment caching? If so – how would I expire these?

Thanks for your help

Scott

I use this and works great http://snippets.dzone.com/posts/show/2975

Gokhan
www.sylow.net
Scott H. wrote:

Hey

I have a sidebar on every page of my site which is part of my layout.
This sidebar contains a list of the last 10 updated threads in the
forum.

I have a ‘setup_sidebar’ action in the application controller and a
‘before_filter’ in every controller to populate the variables in the
sidebar.

My question is – how on Earth do I cache this sidebar??

Would I use fragment caching? If so – how would I expire these?

Thanks for your help

Scott

Gokhan A. wrote:

I use this and works great http://snippets.dzone.com/posts/show/2975

Gokhan
www.sylow.net
Scott H. wrote:

thanks, but I don’t want the expiry to be time based, I need to expire
it when a new post is added to the forum (which I know how to do if I
use fragment caching on a single page - just not if the fragment is in
the layout)

Chris T wrote:

Scott H. wrote:

My question is � how on Earth do I cache this sidebar??

Would I use fragment caching? If so � how would I expire these?

Thanks for your help

Scott

Yup, fragment caching’s the way to go. I use the timed_fragment_caching
plugin. Obviously if the sidebar is common across controllers (e.g. a
tag_cloud) you don’t need to generate a cached copy for each of the
controllers. See the Rails Envy blog for an excellent run through on
fragment caching –
http://www.railsenvy.com/2007/3/20/ruby-on-rails-caching-tutorial-part-2

Hope this helps
Chris

Thanks,

But if I use fragment caching in the layout, a new sidebar-cache-file
will be created for every page of my site as the sidebar features on
every page on my site. So how do i expire all of these at once?

Scott H. wrote:

My question is – how on Earth do I cache this sidebar??

Would I use fragment caching? If so – how would I expire these?

Thanks for your help

Scott

Yup, fragment caching’s the way to go. I use the timed_fragment_caching
plugin. Obviously if the sidebar is common across controllers (e.g. a
tag_cloud) you don’t need to generate a cached copy for each of the
controllers. See the Rails Envy blog for an excellent run through on
fragment caching –
http://www.railsenvy.com/2007/3/20/ruby-on-rails-caching-tutorial-part-2

Hope this helps
Chris

Scott H. wrote:

Scott
Hope this helps

Not if you name the cache properly. Have a look at the Rails Envy post,
particularly “Advanced Naming with the Fragment Cache”

You could have a look at this post…
http://rails.co.za/articles/2006/11/28/new-ruby-on-rails-plugin-cache_filter

Scott H. wrote:

<% cache (:controller => “base”, :action => “sidebar”) do %>

Yup. You can also just give the sidebar a name rather than using the
controller/action-type hash. Something like “recent_posts”, or even
“sidebars/recent_posts”, and another one “sidebar/tag_cloud” and these
will be put in a sidebar folder in the cache directory. I’m not sure one
is better than the other, but the second way seemed a bit more logical
to me somehow.

Not if you name the cache properly. Have a look at the Rails Envy post,
particularly “Advanced Naming with the Fragment Cache”

ah! Ok, I see what he’s saying - you can just use a fake controller and
action for the sake of naming conventions.

So does this mean if i have a fragment cache named…

<% cache (:controller => “base”, :action => “sidebar”) do %>

and if I use…

expire_fragment(:controller => ‘base’, :action => ‘sidebar’)

…when a user adds a new post, it will expire all the cache to do with
the sidebar throughout my entire site?

Scott H. wrote:

and the sidebar has the following fragment cache in the application

I believe so, but while your building the app you can always turn the
caching on in the development environment (but remember to turn it off
later too) to see what cached files are produced. I also use the
cache-test plugin to test my caching . It can get a bit complicated when
you’ve got user-specific cached fragments that have a number of ways to
get expired.

Thanks so much for you help

just another question…

If a user loads the sidebar for the first time on the ‘contact us’ and
the ‘about us’ page with the following URL’s

mysite.com/info/contact
mysite.com/company/about

and the sidebar has the following fragment cache in the application
layout

<% cache (:controller => “base”, :action => “sidebar”) do %>

Will rails only generate ONE file ie…

/tmp/cache/mysite.com/base/sidebar.cache