Forum: Ruby on Rails memcached fails to retrieve object in a different controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2007-03-05 20:05
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
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2007-03-05 20:13
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
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2007-03-05 20:31
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
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2007-03-05 20:56
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
This topic is locked and can not be replied to.