Thursday, October 28, 2010

Memcached undefined class/module in development environment

I had some trouble tracking down the cause of a undefined class/module error from the memcache client so I thought I would share my findings.

The memcache-client uses the ruby core Marshal.load to retrieve the cached values from the memcached server. In this context, Marshal.load does not have the benefit of the Rails class-loader, which results in the above error.

To fix:

  1. Set cache_classes=true. This is probably already set to true in every environment except development. The class will have already been cached before the Marshal.load if caching is turned on.
  2. Make sure that cached classes are referenced in the controller before cache retrievals are attempted. This is usually accomplished with a before_filter in the application_controller.rb
  3. Leave cache_classes=false and set the cache_store to a server:port that is not running memcached. When rails encounters the caching block, it will try to fetch from cache and find nothing because it cannot connect to the memcached server. It will then execute the code inside the cache block successfully.

Option 1 is is not desirable because server restarts are required for clients to see updated server classes. I really don't like option 2 because we are adding development-only code to production code. I like option 3 because I don’t need memcached running in development mode.

The relevant parts of my development.rb:

config.cache_classes = false

config.cache_store = :mem_cache_store, '', {:namespace => "dev"}


Comments: Post a Comment

Subscribe to Post Comments [Atom]

<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]