# Expire\_fragment from observer (not sweeper!)

**URL:** <https://www.ruby-forum.com/t/expire-fragment-from-observer-not-sweeper/92038>\
**Category:** Rails\
**Created:** [April 15, 2007, 2:02am UTC](https://www.ruby-forum.com/t/expire-fragment-from-observer-not-sweeper/92038 "2007-04-15T02:02:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [April 15, 2007, 2:02am UTC](https://www.ruby-forum.com/t/expire-fragment-from-observer-not-sweeper/92038/1 "2007-04-15T02:02:09Z")

</div>

Hi all,

I’m trying to expire a cached fragment when a model is updated. The  
usual way to do this is with a sweeper, but sweepers only work when an  
action is being called at the same time - I need to expire a cached  
fragment during some back-end batch process.

A normal observer should work, but you don’t have access to the  
expire\_fragment method from there.

Adding “include ActionController::Caching::Fragments” in my observer  
gives me access this method, but re-runs the fragment\_cache\_store  
initialise method and resets my config to the default MemoryStore.

I have a few options, (such as: copy and paste the expire\_fragment  
method into my observer, reconfig the fragment\_cache\_store after  
inclusion, make my batch process make a http call to a controller  
action to kill the cache), but these all seem pretty ugly.

Does anyone know if there is an easier way to get access to this  
method from within my observer?

Thanks,

## John.

[http://johnleach.co.uk](http://johnleach.co.uk)
