views:

367

answers:

1

There are currently two problems with storing ActiveRecord objects in memcached.

  1. The Undefined Class/Module problem (Google search). From what I've read up, this is still a bug that nobody has a real good solution for. The cache_fu plugin has probably the best solution for this, wrapping its retrieve call in a block that tries to catch this error, parses the message and tries to load the undefined class/module.

  2. The infamous LH ticket #1339 (LH Ticket). This bug will only happen when you have cache_classes set to FALSE (development, test).

After googling for weeks, I still haven't found a good technique for storing AR instances in memcached without having to deal with the 2 issues listed above.

The idea I haven't tried yet is removing the attributes from the instance as strings (just how AR receives them from the DB before it does its type casting), storing those in memcached and then on retrieval from the cache, somehow instantiate an AR object using these values. Is this possible? If so, what is the best way to do it?

I'm just looking for ways other Rails developers have tackled this problem.

+1  A: 

In our projects we store the object as XML.

cache.write(user.cache_key, user.to_xml) # write to cache
User.new(Hash.from_xml(cache.read(cache_key))) # reach from cache xml

There is some extra for serializing/de-serializing XML. But this has enabled us to share the cache amongst non Ruby apps.

KandadaBoggu
I like the idea, will give it a try
Jose Fernandez
I'd probably go with JSON at this point. In any case, I've found storing anything but strings, ints, and arrays of the same in memcached is madness.
Barry Hess