Recipes Changelog

This is my first "real" Rails project, so I'm writing about every little thing.  Today's new features:  Paging of the category list, and caching.

Rails's page caching is very straightforward.  You enable config.action_controller.perform_caching in config/environments/development.rb, and add a directive to the page controller to tell it which actions it can cache.

So in my case, in app/controllers/recipe_controller.rb I added:

class RecipesController < ApplicationController
  caches_page :categories, :category

I'm caching two actions here, showing the Categories page, and showing the pages within each category.

Some categories contain thousands of recipes, so it was important that I implement paging.  Paging and caching will interact badly if you use a parameter on a URL to say what page to show, so it's important to use a parameter within the URL itself and not a parameter to specify which page to show.  For example:

Bad:

http://recipes.stevex.net/category/Appetizers?page=2

Good:

http://recipes.stevex.net/category/Appetizers/2

Rails simply dumps the cache file into the public directory, at the exact URL you'll use to reference it - so the cached page for this ends up being

public public/category/Appetizers/2.html

When you request /category/Appetizers/2, if this file exists, it gets delivered; otherwise it gets generated, cached and delivered.  Want to flush the cache?  Delete the disk files.

Caching is a big incentive to have these pages remain static, so when I add user login capability, I probably won't show the logged-in user's name on these pages.