# Conditional expire\_fragment

**URL:** <https://www.ruby-forum.com/t/conditional-expire-fragment/154250>\
**Category:** Rails\
**Created:** [November 26, 2008, 6:51pm UTC](https://www.ruby-forum.com/t/conditional-expire-fragment/154250 "2008-11-26T18:51:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![indigoi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b3f665/32.png) [@indigoi](https://www.ruby-forum.com/u/indigoi)\
**Post date:** [November 26, 2008, 6:51pm UTC](https://www.ruby-forum.com/t/conditional-expire-fragment/154250/1 "2008-11-26T18:51:18Z")

</div>

I just recently implemented caching on my site, and i have a sweeper  
attached to my :update action, to clear my index (which shows top ranked  
items). Is it possible to first check to see if there is a change, and  
only expire the cache when appropriate??

my index action looks like this:

def index  
unless read\_fragment({:page =\> params[:page] || 1})  
@new\_phrases = Phrase.find\_top(params[:page]||1)  
end  
end

I want my sweeper action to look something like this:

def expire\_cache(phrase)  
if read\_fragment({:action =\> ‘index’, :page =\> 1}) ==  
Phrase.find\_top(1)  
1.upto(5) {|i| expire\_fragment(:action =\> ‘index’, :page =\> i  
) }  
end  
end

Rails 2.1.0, ruby 1.8.6. Is this type of behavior possible or reasonable  
to implement??
