Workaround the Memcached "undefined class/module" Bug
Posted by Chris Blackburn Thu, 31 May 2007 18:10:00 GMT
If you are experiencing the error: “undefined class/module MyClass” when fetching data from memcached, be assured that you are not alone. It is a known bug and the simplest way I know of to get around it is to reference the class or classes right before you retrieve data from the cache.
For example, if the following code causes the problem:
if not (genres = Cache.get(key))
genres = Genre.find(:all, :condition => "platform_id = 1")
Cache.put(key, genres, 60*60*24) # cache for 1 day
end… then this code will work around it:
Genre
if not (genres = Cache.get(key))
genres = Genre.find(:all, :condition => "platform_id = 1")
Cache.put(key, genres, 60*60*24) # cache for 1 day
endNotice the ‘Genre’ reference before the if statement. Some have reported success by using the ‘model’ statement within the controller, however that is deprecated. This workaround will get you going again.

i encounter thie problem also. how dit you fix it?
Vincent, you can fix it by simply accessing the class. Notice the second block of code above. before the ‘if’ statement I have accessed the class by just referring to it ‘Genre’
Thanks for the workaround. Any idea if this is being addressed or has been addressed in future future releases?
A nifty fix, much cleaner than preloading a list of models.
This does not work well single table inheritance cases, however.
What if Cache.get(‘User_1’) returns and AdminUser, for instance?
Luke, I haven’t run into the problem lately.
Reid, the thing about memcached, or any other cache, is that whatever you put in comes back out. Any type-checking would be left up to you.
So let’s say you put an AdminUser into the cache as ‘User_1’. When you retrieve it you can do:
I assume this is what you were thinking. Let me know if I am not getting your point.