# Best practice for fragment caching a dynamic listing?

**URL:** <https://www.ruby-forum.com/t/best-practice-for-fragment-caching-a-dynamic-listing/107097>\
**Category:** Rails\
**Created:** [June 18, 2007, 3:25am UTC](https://www.ruby-forum.com/t/best-practice-for-fragment-caching-a-dynamic-listing/107097 "2007-06-18T03:25:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![openmind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6de8d8/32.png) [@openmind](https://www.ruby-forum.com/u/openmind)\
**Post date:** [June 18, 2007, 3:25am UTC](https://www.ruby-forum.com/t/best-practice-for-fragment-caching-a-dynamic-listing/107097/1 "2007-06-18T03:25:50Z")

</div>

Hi,

I’m new to fragment caching, and was wondering the best way to do it for  
a dynamically generated page. I have been working off of the “Agile Web  
Development with Rails” book, but it’s rather brief on fragment caching.

For example, suppose you have a list of manufacturers and each of which  
has many products. Here is how I would implement the caching, but I’m  
not clear if it will work, or if there’s a cleaner, less clunky of doing  
it:

Product Controller:  
def list  
@mid = params[:manufacturer\_id]

if @mid.nil?  
@mid = “all”  
unless read\_fragment(:action =\> ‘list’, :mid =\> “all”)  
@products = Product.find(:all)  
end  
else  
unless read\_fragment(;action =\> ‘list’, :mid =\> @mid.to\_s)  
@products = Product.find\_by\_manufacturer\_id(@mid)  
end  
end

end

View (list.rhtml)  
\<% cache(:action =\>‘list’, :mid =\> @mid) do %\>

\<% for p in @products %\>- \<%= p.name %\>
\<% end %\>
\<% end \>

Any suggestions or advice on a better way to to go about doing this  
would be much appreciated. Thanks.

---

<div class="post-metadata">

**Author:** ![openmind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6de8d8/32.png) [@openmind](https://www.ruby-forum.com/u/openmind)\
**Post date:** [June 18, 2007, 3:14pm UTC](https://www.ruby-forum.com/t/best-practice-for-fragment-caching-a-dynamic-listing/107097/2 "2007-06-18T15:14:52Z")

</div>

Jay,

I’m fairly new to Rails too, but the code looks fine. There are no doubt  
more Ruby ways of writing it.

One thing. Doesn’t the cache need to be expired when a manufacture  
updates?

Xin

---

<div class="post-metadata">

**Author:** ![openmind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6de8d8/32.png) [@openmind](https://www.ruby-forum.com/u/openmind)\
**Post date:** [June 18, 2007, 4:37pm UTC](https://www.ruby-forum.com/t/best-practice-for-fragment-caching-a-dynamic-listing/107097/3 "2007-06-18T16:37:48Z")

</div>

Xin Z. wrote:

> Jay,
> 
> I’m fairly new to Rails too, but the code looks fine. There are no doubt  
> more Ruby ways of writing it.
> 
> One thing. Doesn’t the cache need to be expired when a manufacture  
> updates?
> 
> Xin

Yes, I didn’t include the code for putting in sweepers to expire the  
cache when the manufacturer/product is updated. Does anyone have any  
recommendations for production-ready plugins that make this simpler?  
I’ve been bouncing around the various plugin sites, and haven’t found  
one that seems to make this as easy as it should be.

One promising one is this one, but I don’t think I would call it  
production-ready, as it looks like it was released _yesterday_:

[http://blog.overlay.tv/articles/2007/06/16/intelligent\_fragment\_cache](http://blog.overlay.tv/articles/2007/06/16/intelligent_fragment_cache)
