# Memcached fails to retrieve object in a different controller

**URL:** <https://www.ruby-forum.com/t/memcached-fails-to-retrieve-object-in-a-different-controller/87760>\
**Category:** Rails\
**Created:** [March 5, 2007, 8:05pm UTC](https://www.ruby-forum.com/t/memcached-fails-to-retrieve-object-in-a-different-controller/87760 "2007-03-05T20:05:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 5, 2007, 8:05pm UTC](https://www.ruby-forum.com/t/memcached-fails-to-retrieve-object-in-a-different-controller/87760/1 "2007-03-05T20:05:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 5, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/memcached-fails-to-retrieve-object-in-a-different-controller/87760/2 "2007-03-05T20:13:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 5, 2007, 8:31pm UTC](https://www.ruby-forum.com/t/memcached-fails-to-retrieve-object-in-a-different-controller/87760/3 "2007-03-05T20:31:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 5, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/memcached-fails-to-retrieve-object-in-a-different-controller/87760/4 "2007-03-05T20:56:25Z")

</div>

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
