Topic: memcached in 2.1 weird problems

Hi, I am trying to use memcached and the rails 2.1 caching stuff, here is what I have done:

 def also_viewed_cached
    Rails.cache.fetch('also_viewed_' + self.id.to_s, :expires_in => 30.minutes) { also_viewed }
end

#... in my environment...
config.cache_store = :mem_cache_store


also_viewed returns an array of active record objects(authors). The first call populates the cache fine, but the 2nd call when getting from the cache cant call methods on the ojbect, it gives me the error:

ActionView::TemplateError (undefined method `display_name' for #<Author:0x2aaaac40db08>) on line #11 of authors/_viewers_also_viewed.html.erb

The problem is this method is there, and when I bring it up in the debugger, I can see its the right class, I can look at attributes and they are there, but any method call causes undefined method error. If I try to get the id of the object, I get a stack level too deep error.

Is there some rule about storing active record objects in memcached using the new 2.1 caching stuff that im not aware of?

Re: memcached in 2.1 weird problems

same prob here, rails 2.3.2

Re: memcached in 2.1 weird problems

I read about this on a blog a while ago, objects get stripped of their methods before they're saved to the cache, and don't get automatically replaced when you retrieve them. I can't for the life of me recall what the solution was though, they had to be reinitialized somehow. I'll try to locate the blog and post a link if I find it.

edit: Couldn't find the blog but after a little tinkering I found the missing methods stay put if you enable class caching in your environment (config.cache_classes = true). It appears to have something to do with the way classes are refreshed on every reload while in development mode.

Last edited by marsvin (2009-06-28 02:07:33)

Re: memcached in 2.1 weird problems

I think I found a workaround of putting the class rightbefore I put into cache, so if putting a car into cache, putting the class name before seemed to work around problem for me.