Memcached fails to retrieve object in a different controller

All,

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
cache:

dg = Cache.get(‘datagrid_3’) {
XSLDatagrid.new(:klass => TargetList,
:find_options => [:all,
{:conditions => [condition_str,
session[:user]]}],
: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
before_filter:

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

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
memcached?

Wes

The fact that there are two controllers is not relevant.

If you do this:

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

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

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?

Thanks,
Wes

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?

Wes

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
done.

Use

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.

Wes