# New to cacheing

**URL:** <https://www.ruby-forum.com/t/new-to-cacheing/186116>\
**Category:** Rails\
**Created:** [March 31, 2010, 7:30pm UTC](https://www.ruby-forum.com/t/new-to-cacheing/186116 "2010-03-31T19:30:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [March 31, 2010, 7:30pm UTC](https://www.ruby-forum.com/t/new-to-cacheing/186116/1 "2010-03-31T19:30:03Z")

</div>

Ok, So, I’m a bad developer and have never used any sort of cacheing  
before. I decided it’s time I step up and I threw in some memcached  
magic. I’m still pretty new to all this, so I’m confused on how some  
things work. I’ve watched the railscast on cacheing, and gone through  
many tutorials.

So, the problem I’m having is that I have this page being cached, and  
it’s not updating the page when I do any sort of CRUD action on the  
model primarily used on this page.

Here is what I have

class SiteController \< ApplicationController  
caches\_action :index

def index  
#this in my home page  
end  
en

class ListingsController \< SiteController  
#This is the page being cached  
def index  
@listings = Listing.all  
end  
end

class ListingSweeper \< ActionController::Caching::Sweeper  
observe Listing

def after\_save(listing)  
expire\_cache(listing)  
end

def after\_update(listing)  
expire\_cache(listing)  
end

def after\_destroy(listing)  
expire\_cache(listing)  
end

def expire\_cache(listing)  
expire\_action root\_path  
expire\_action listings\_path  
end

end

When I create a new Listing, or update or destroy an existing one, it  
should clear the cache of the ListingsController#index, right?

When I go the the page, and refresh over and over, this is what pops up  
in my production.log

Processing ListingsController#index (for 12.34.567.58 at 2010-03-31  
17:13:29) [GET]  
Filter chain halted as  
[#\<ActionController::Filters::AroundFilter:0x2aaaae2804c8  
@options={:if=\>nil, :unless=\>nil, :only=\>#\<Set: {“contact”, “index”}\>},  
@method=#Proc:0x00002aaaac2a3ec0@/var/rails/app/releases/20100331171209/vendor/rails/actionpack/lib/action\_controller/caching/actions.rb:64,  
@kind=:filter, @identifier=nil\>] did\_not\_yield.  
Completed in 2ms (View: 0, DB: 0) | 200 OK  
[[http://245.254.135.24/listings](http://245.254.135.24/listings)]

Anyone have a good link for a tutorial I can get, or maybe an idea or  
what I might be missing?

Any help is much appreciated!

Thanks,  
~Jeremy

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [March 31, 2010, 8:43pm UTC](https://www.ruby-forum.com/t/new-to-cacheing/186116/2 "2010-03-31T20:43:01Z")

</div>

Another odd thing maybe someone can clear up for me.

In my sweepers I currently have:

def expire\_cache  
expire\_action root\_path  
end

but my cache isn’t being expired, so I figured I would put the route in  
manually

def expire\_cache  
expire\_action :controller =\> ‘site’, :action =\> ‘index’  
end

and I get this error:

ActionController::RoutingError (No route matches  
{:controller=\>“admin/site”, :action=\>“index”}):

Now, obviously I’m updating the model in the admin section, but I’m not  
specifying my controller to be ‘admin/site’. I just want to update  
object in the admin section, and update the cache on the front end of  
the site.

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [March 31, 2010, 8:47pm UTC](https://www.ruby-forum.com/t/new-to-cacheing/186116/3 "2010-03-31T20:47:56Z")

</div>

Have you added the sweeper to your list of observers in config/  
environment.rb?

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [March 31, 2010, 8:51pm UTC](https://www.ruby-forum.com/t/new-to-cacheing/186116/4 "2010-03-31T20:51:17Z")

</div>

David wrote:

> Have you added the sweeper to your list of observers in config/  
> environment.rb?

Actually, I had not. I had another sweeper, and my user observer in  
there. I will try that out, and post back what happens 🙂

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [March 31, 2010, 8:53pm UTC](https://www.ruby-forum.com/t/new-to-cacheing/186116/5 "2010-03-31T20:53:06Z")

</div>

Jeremy W. wrote:

> David wrote:
> 
> > Have you added the sweeper to your list of observers in config/  
> > environment.rb?
> 
> Actually, I had not. I had another sweeper, and my user observer in  
> there. I will try that out, and post back what happens 🙂

fail ☹

config.active\_record.observers = :user\_observer, :listing\_sweeper

This is what I should have, right?

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [April 1, 2010, 11:39pm UTC](https://www.ruby-forum.com/t/new-to-cacheing/186116/6 "2010-04-01T23:39:44Z")

</div>

Quy Doan wrote:

> Have you added sweeper path ?  
> config.load\_paths += %W( #{RAILS\_ROOT}/app/sweepers)

Yes, and they seem to load ok. I don’t get any errors for uninitialized  
constants or anything.

Environment.rb  
RAILS\_GEM\_VERSION = ‘2.3.5’ unless defined? RAILS\_GEM\_VERSION  
require File.join(File.dirname( **FILE** ), ‘boot’)  
Rails::Initializer.run do |config|  
config.load\_paths \<\< “#{RAILS\_ROOT}/app/sweepers”  
config.active\_record.observers = :user\_observer, :listing\_sweeper  
end

Production.rb  
config.cache\_classes = true  
config.action\_controller.consider\_all\_requests\_local = false  
config.action\_controller.perform\_caching = true  
config.action\_view.cache\_template\_loading = true  
config.cache\_store = :mem\_cache\_store

config/initializers/cache\_money.rb  
if RAILS\_ENV != ‘development’  
require ‘cache\_money’  
config = YAML.load(IO.read(File.join(RAILS\_ROOT, “config”,  
“memcached.yml”)))[RAILS\_ENV]  
$memcache = MemCache.new(config)  
$memcache.servers = config[‘servers’]  
$local = Cash::Local.new($memcache)  
$lock = Cash::Lock.new($memcache)  
$cache = Cash::Transactional.new($local, $lock)  
class ActiveRecord::Base  
is\_cached :repository =\> $cache  
end  
else  
class ActiveRecord::Base  
def self.index(\*args)  
end  
end  
end

Thanks for all the suggestions. Please, if anyone else has any shoot 'em  
my way. I’m running late on the app, so I might have to just drop the  
cache part (which I don’t want to do).

Thanks,  
~Jeremy

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [April 1, 2010, 8:26pm UTC](https://www.ruby-forum.com/t/new-to-cacheing/186116/7 "2010-04-01T20:26:44Z")

</div>

Have you added sweeper path ?  
config.load\_paths += %W( #{RAILS\_ROOT}/app/sweepers)

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [May 15, 2010, 2:25am UTC](https://www.ruby-forum.com/t/new-to-cacheing/186116/8 "2010-05-15T02:25:59Z")

</div>

Hi,

Jeremy W. wrote:

> Ok, So, I’m a bad developer and have never used any sort of cacheing  
> before. I decided it’s time I step up and I threw in some memcached  
> magic. I’m still pretty new to all this, so I’m confused on how some  
> things work. I’ve watched the railscast on cacheing, and gone through  
> many tutorials.
> 
> So, the problem I’m having is that I have this page being cached, and  
> it’s not updating the page when I do any sort of CRUD action on the  
> model primarily used on this page.
> 
> Here is what I have
> 
> class SiteController \< ApplicationController  
> caches\_action :index
> 
> def index  
> #this in my home page  
> end  
> en
> 
> class ListingsController \< SiteController  
> #This is the page being cached  
> def index  
> @listings = Listing.all  
> end  
> end
> 
> class ListingSweeper \< ActionController::Caching::Sweeper  
> observe Listing
> 
> def after\_save(listing)  
> expire\_cache(listing)  
> end
> 
> def after\_update(listing)  
> expire\_cache(listing)  
> end
> 
> def after\_destroy(listing)  
> expire\_cache(listing)  
> end
> 
> def expire\_cache(listing)  
> expire\_action root\_path  
> expire\_action listings\_path  
> end
> 
> end

It seems to me that you miss to specify the cache sweeper in the  
controller.  
In your case, try to add this:

class SiteController \< ApplicationController  
caches\_action :index  
cache\_sweeper :listing\_sweeper

Regards,

.Viet Trung.
