# Pagination and caching

**URL:** <https://www.ruby-forum.com/t/pagination-and-caching/195167>\
**Category:** Rails\
**Created:** [September 18, 2010, 1:52am UTC](https://www.ruby-forum.com/t/pagination-and-caching/195167 "2010-09-18T01:52:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [September 18, 2010, 1:52am UTC](https://www.ruby-forum.com/t/pagination-and-caching/195167/1 "2010-09-18T01:52:58Z")

</div>

What’s the best way to cache a paginated result set with rails and  
memcached.

For example

#posts controller

```
def index
@posts = Rails.cache.fetch('all_posts') do
Post.paginate(:conditions => ['xx = ?', yy], :include =>

```

[:author], :page =\> params[:page], :order =\> ‘created\_at D.ESC’)  
end  
end

This obviously doesn’t work when the params[:page] changes…I can  
change the key to “all\_posts\_#{params[:page]}  
_#{params[:order]_#{Post.last.created\_at.to\_i}}”

But then there could be several possible order (recent, popular, most  
voted etc) and there will be a combination of pages and orders…lots  
of keys this way.

Problem #2 - It seems when I implement this solution the caches get  
written correctly and the page loads fine during the first call do a  
paginate action but when I click back on the same page i.e. page1,  
with “recent” order, it seems the browser does not even make a call to  
the server. I don’t see any controller action being called in the  
production log??

I am using passenger, REE, memcached, and rails 2.3.5. Firebug shows  
no requests being made…

Is there a simples/more graceful way of handling this?

Thanks
