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
end

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

Tags , , , , , ,  | 5 comments | no trackbacks

Comments

  1. vincent said 4 months later:

    i encounter thie problem also. how dit you fix it?

  2. CB said 5 months later:

    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’

  3. Luke said about 1 year later:

    Thanks for the workaround. Any idea if this is being addressed or has been addressed in future future releases?

  4. Reid said about 1 year later:

    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?

  5. Chris Blackburn said about 1 year later:

    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:

    u = Cache.get(‘User_1’)
    if u.is_a?(AdminUser)
      # If we get here u is indeed an AdminUser.
    end

    I assume this is what you were thinking. Let me know if I am not getting your point.

Trackbacks

Use the following link to trackback from your own site:
http://blog.cbciweb.com/articles/trackback/28

(leave url/email »)

   Comment Markup Help Preview comment