# Fragment caching and pagination

**URL:** <https://www.ruby-forum.com/t/fragment-caching-and-pagination/99592>\
**Category:** Rails\
**Created:** [January 31, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/fragment-caching-and-pagination/99592 "2006-01-31T23:48:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [January 31, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/fragment-caching-and-pagination/99592/1 "2006-01-31T23:48:28Z")

</div>

I’m just getting my hands dirty with caching. The pages I want to cache  
have  
some user specific data on them (for example “log out”). That sent me  
down  
the route of fragment caching. One of the things I want to pagination is  
a  
paginated list. My question is how is pagination and query strings  
handled  
with fragment caching? Does each page get its own fragment and query  
string  
identifier?

thanks,

Steve  
[http://www.smarkets.net](http://www.smarkets.net)

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [February 1, 2006, 1:49pm UTC](https://www.ruby-forum.com/t/fragment-caching-and-pagination/99592/2 "2006-02-01T13:49:53Z")

</div>

Steve  
\>… how is pagination and query strings handled with fragment  
caching?  
\> Does each page get its own fragment and query string identifier?

Yes. Each page related fragment is cached separately.

Here is what works for me:

In the view: (event/list.rhtml, a paginated view, with an optional  
‘year’ filter. )

```
line 1: <% cache(:controller => 'event', :action => 'list', :group

```

=\> visitor.group, :page =\> params[:page], :year =\> params[:year]) do%\>  
…

In the Controller:  
class EventController \< ApplicationController  
cache\_sweeper :event\_sweeper, :only =\> [:edit, :update, :new,  
:create, :destroy]  
…

In the sweeper:  
class EventSweeper \< ActionController::Caching::BaseSweeper  
observe Event

```
    def after_save(record)
        expire_fragment %r{event/list.*}
        expire_home_page
    end
    def after_destroy(record)
        expire_fragment %r{event/list.*}
        expire_home_page
    end

```

end

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [February 1, 2006, 1:49pm UTC](https://www.ruby-forum.com/t/fragment-caching-and-pagination/99592/3 "2006-02-01T13:49:53Z")

</div>

Excellent. Just what I was looking for.

Steve
