# Caching

**URL:** <https://www.ruby-forum.com/t/caching/194606>\
**Category:** Rails\
**Created:** [September 7, 2010, 10:02pm UTC](https://www.ruby-forum.com/t/caching/194606 "2010-09-07T22:02:46Z")\
**Posts on this page:** 3\
**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:** [September 7, 2010, 10:02pm UTC](https://www.ruby-forum.com/t/caching/194606/1 "2010-09-07T22:02:46Z")

</div>

What is the best way to set up caching when GET parameters are  
involved? For example I don’t want both /index and /index?  
tags=example to be cached as index.html. Does that make sense?

Thanks,  
Tom

---

<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:** [September 7, 2010, 11:32pm UTC](https://www.ruby-forum.com/t/caching/194606/2 "2010-09-07T23:32:37Z")

</div>

You can always use a fragment caching approach and name your fragments  
appropriately. Suppose its the index view of Projects…

def index

# massage your params tags into a usable form (implementation

# is left as an exercise for the reader - ‘example’ in a simple

# case, or some ugly string if you support multiple tags, and

# perhaps “base” if no tags exist in params)

@param\_string = massage\_tags(params[:tags])

# if you have it cached, don’t bother with the read

unless Rails.cache.exist?(‘views/project.index.’+@param\_string)  
@projects = Project.find(blah blah blah)  
end  
end

index.html.erb

\<% cache(‘project.index.’+@param\_string) do %\>  
\<% @projects.each do |project| %\>  
blah blah blah  
\<% end %\>  
\<% end %\>

Pagination will throw a wrinkle in this, as you’ll want a cache per page  
possibly, and create/edit/delete of an arbitrary project will  
essentially invalidate all the caches for that model, unless you do some  
logic I don’t even want to think about.

To save some processing time, you could cache just the index row for  
each project and save that rendering time. If you display 30 projects on  
the index form, and only 1 on the current page has changed (either via  
addition, editing, or deletion), you’ll at least save the rendering on  
the other 39… whether that is worthwhile depends on what data you are  
showing.

YMMV

---

<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:** [September 7, 2010, 11:34pm UTC](https://www.ruby-forum.com/t/caching/194606/3 "2010-09-07T23:34:33Z")

</div>

Ar Chron wrote:  
Er, ignore my math.  
30 per page, edit/change 1, save rendering on 29, not 39… Doh!
