Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1

176 | Chapter 6: Performance


db_memcache_store, which uses memcached as a cache on top of database sessions,
so sessions will not be lost when the memcached server is stopped. It is still slow, but it
looks promising. It can be installed as a Rails plugin fromhttp://topfunky.net/svn/
plugins/db_memcache_store/.


CookieStore


By default, Rails 2.0 assumes that the session will be small (usually a user ID and any
flashdata) and stores the entire session in a client-side cookie. This removes a
server-side obligation to keep track of sessions, but it introduces some minor secu-
rity issues. The issues surrounding the cookie session store were discussed in detail
in Chapter 5.


Session management


Of course, the ultimate performance enhancement to sessions is not having to use
them at all. Thesessionclass method allows you to specify actions for which ses-
sions are not needed at all. This can save a decent amount of processing power for
actions that are viewed often but do not require the use of a session.


class PersonController < ApplicationController
session :off, :only => [ :list, :show ]
end

Caching


Database caching viacached_modelandacts_as_cachedhas been discussed earlier in
the book. If your bottleneck is in retrieving records from a database, memcached and
those solutions will help.


There is another significant bottleneck in most applications, though, and this one
requires more thought. Many applications have rendered pages or parts of rendered
pages that, while dynamic, can be cached to some extent. Rails has a few different
types of caching methods that allow various levels of granularity when deciding what
to cache.


By default, caching is only enabled in production mode, so as not to complicate the
debugging process. You can enable caching in development mode with the following
line inconfig/environments/development.rb:


config.action_controller.perform_caching = true

Many Rails plugins are available that modify or improve the standard caching func-
tionality. The Agile Web Development plugins database (http://agilewebdevelopment.
com/plugins) is the best place to look if you have a specific need.

Free download pdf