RoR caching and routing

I’ve been trying to learn Ruby on Rails. So far I’ve found it to be a very elegant solution to the problem of writing web apps. Until now I’ve used Tango or PHP, but I hope that I never have to use them again.

I recently had a rather interesting problem, however. My first teaching project was a little app to generate a bookmarks page, and it allows me to categorize bookmarks. Relatively straightforward, right? Well, I got the code to handle logging in, the categories, subcategories and bookmarks done just fine with no hitches, but then I wanted to test out caching.

In development.rb I set caching to true, and then enable page caching on my “view” controller &emdash; the one that is not behind the login and simply outputs the bookmarks list. I reload the page, and presto, the index.html page magically appears in my app’s public folder. “Sweet,” I think. I then edit one of the pieces of bookmark data, and my sweeper class kicks in, logs that it is expiring the page, and then expires the page.

  def expire_view(model)
    model.logger.info("Expiring the cached index")
    expire_page(:controller => "view", :action => "index")
  end

Except that the page doesn’t actually expire. What the f*@#!?!

After several days of poking around, trying action caching instead, and a few posts to various forums, I figured out what the problem was. I had reconfigured my routes.rb to essentially point all bogus URL’s to the view::index action, which was the page being cached. After I changed routes.rb back, the page started expiring. I’m guessing that :caches_page and expire_page() only work when there are single URL’s pointing to the page being cached. Has anybody else had a similar experience?

No comments to date

Subscribe to comments

Leave a Reply