New to cacheing

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]

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

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.

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

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 :slight_smile:

Jeremy Woertink 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 :slight_smile:

fail :frowning:

config.active_record.observers = :user_observer, :listing_sweeper

This is what I should have, right?

Have you added sweeper path ?
config.load_paths += %W( #{RAILS_ROOT}/app/sweepers)

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

Hi,

Jeremy Woertink 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.