cache a sidebar in the layout

Scott Holland 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

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

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

Scott Holland wrote:

Chris T wrote:
  

Scott Holland 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?

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

Scott Holland wrote:

  

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?

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.

Scott Holland wrote:

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

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.