Memcached fails to retrieve object in a different controller


I’ve successfully set up memcache client in my Rails app. However, when
I put something into the cache in one controller, and then attempt to
retrieve it in another controller, I get an error.

More detail:

In ListManagementController, I place the item in the cache - by using
the fact that a missing key will put the result of the block into the

dg = Cache.get(‘datagrid_3’) { => TargetList,
:find_options => [:all,
{:conditions => [condition_str,
:columns => columns,
:buttons => buttons,
:special_data => nil,
:buttons_first => true,
:sortable_columns => true,
:total_description => ‘Total lists’,
:enable_scrolling => false)

I can pull this cached value out of the cache without a problem in the
same method.

However, when I move to DatagridController, which has this method as a

def get_datagrid
@datagrid = Cache.get(XSLDatagrid.get_cache_key(session[:user]))

I get the following error in the log:

MemCache Error: undefined class/module TargetList

and I am unable to retrieve the object.

Is there some problem serializing/deserializing Ruby class objects in


The fact that there are two controllers is not relevant.

If you do this:

def put_into_cache
x = Cache.get(‘test3’) {Target}

def get_from_cache
y = Cache.get(‘test3’)

you will get this error: MemCache Error: undefined class/module Target
when you attempt to run “get_from_cache”.

Is this a known issue with the memcache client?


The error is occurring in the call to Marshal.load in memcache.rb (in
the memcache-client gem).

If I put an object containing a class into the cache, I can successfully
retrieve it within the same action.

However, if I try to retrieve it in a different action, it fails as
described above.

Seems like some sort of scoping issue.

Any ideas?


SOLUTION: Add “model” calls to the appropriate controllers that would be
involved in pulling these objects from the cache. This will ensure that
the appropriate class definitions are available when the unmarshaling is


model :target_list, :target, :job

to handle the TargetList, Target, and Job classes.

In my case, I chose to add the model call to application.rb, since
multiple controllers pull my objects from the cache.