What is the best way to store an ActiveRecord object in memcached?

喜夏-厌秋 提交于 2019-12-05 23:57:54

问题


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:


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 cost for serializing/de-serializing XML. But this has enabled us to share the cache amongst non Ruby apps.



来源:https://stackoverflow.com/questions/2393974/what-is-the-best-way-to-store-an-activerecord-object-in-memcached

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!