# Cache sweeper not getting called

**URL:** <https://www.ruby-forum.com/t/cache-sweeper-not-getting-called/85526>\
**Category:** Rails\
**Created:** [February 14, 2007, 8:27am UTC](https://www.ruby-forum.com/t/cache-sweeper-not-getting-called/85526 "2007-02-14T08:27:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@dang](https://www.ruby-forum.com/u/dang)\
**Post date:** [February 14, 2007, 8:27am UTC](https://www.ruby-forum.com/t/cache-sweeper-not-getting-called/85526/1 "2007-02-14T08:27:52Z")

</div>

Hi,

I’ve started to implement page caches but I’ve hit a brick wall getting  
a sweeper to clear the cache when needed. It seems that any models that  
I tell it to observe aren’t being observed properly. If I add the  
sweeper to a controller the initialize method runs, but no matter what I  
do the after\_save/after\_update callbacks aren’t running. Also if I try  
to call “expire\_page”, I get a method missing error. It seems like the  
class isn’t actually extending the Sweeper at all, is there something  
I’m missing??

Here’s the sweeper code:

class PublishSweeper \< ActionController::Caching::Sweeper

observe Program

def initialize  
puts “sweeper initialised”  
end

def after\_save(record)  
puts “The sweeper knows that node: #{record.inspect} has been  
saved!”  
end

def after\_update(record)  
puts “The sweeper knows that node: #{record.inspect} has been  
updated!”  
end

end

---

<div class="post-metadata">

**Author:** ![dang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@dang](https://www.ruby-forum.com/u/dang)\
**Post date:** [February 14, 2007, 8:36am UTC](https://www.ruby-forum.com/t/cache-sweeper-not-getting-called/85526/2 "2007-02-14T08:36:47Z")

</div>

Daniel G. wrote:

> Hi,
> 
> I’ve started to implement page caches but I’ve hit a brick wall getting  
> a sweeper to clear the cache when needed. It seems that any models that  
> I tell it to observe aren’t being observed properly. If I add the  
> sweeper to a controller the initialize method runs, but no matter what I  
> do the after\_save/after\_update callbacks aren’t running. Also if I try  
> to call “expire\_page”, I get a method missing error. It seems like the  
> class isn’t actually extending the Sweeper at all, is there something  
> I’m missing??

Did you tell your controller to use the sweeper?

class PublishController \< ApplicationController  
cache\_sweeper :publish\_sweeper  
end

---

<div class="post-metadata">

**Author:** ![dang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@dang](https://www.ruby-forum.com/u/dang)\
**Post date:** [February 15, 2007, 12:10am UTC](https://www.ruby-forum.com/t/cache-sweeper-not-getting-called/85526/3 "2007-02-15T00:10:55Z")

</div>

Yep, I’ve got the sweeper being used in a couple of controllers - all  
of them initialize it, but no model changes are being observed (or at  
least the sweeper isn’t being alerted to the model changes)

---

<div class="post-metadata">

**Author:** ![dang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@dang](https://www.ruby-forum.com/u/dang)\
**Post date:** [February 17, 2007, 4:59pm UTC](https://www.ruby-forum.com/t/cache-sweeper-not-getting-called/85526/4 "2007-02-17T16:59:43Z")

</div>

Did you tell config/environment.rb where and how to load the sweepers?  
That  
one bite me a while back. If you didn’t add it to config.load\_paths in  
the  
Rails::Initializer block there. Hope that helps.

RSL

---

<div class="post-metadata">

**Author:** ![dang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@dang](https://www.ruby-forum.com/u/dang)\
**Post date:** [February 18, 2007, 11:24pm UTC](https://www.ruby-forum.com/t/cache-sweeper-not-getting-called/85526/5 "2007-02-18T23:24:48Z")

</div>

Russell N. wrote:

> Did you tell config/environment.rb where and how to load the sweepers?  
> That  
> one bite me a while back. If you didn’t add it to config.load\_paths in  
> the  
> Rails::Initializer block there. Hope that helps.
> 
> RSL

Thanks for the responses, I think I’m going to blame webrick - even when  
I configrated (my new favourite made up word) caching in dev mode,  
webrick didn’t play nice. Switching to mongrel seems to make it happy.
