[英]What is the best way to store an ActiveRecord object in memcached?
There are currently two problems with storing ActiveRecord objects in memcached. 目前在memcached中存储ActiveRecord对象存在两个问题。
The Undefined Class/Module problem ( Google search ). 未定义的类/模块问题( Google搜索 )。 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.
cache_fu插件可能是最好的解决方案,将其检索调用包装在试图捕获此错误的块中,解析消息并尝试加载未定义的类/模块。
The infamous LH ticket #1339 ( LH Ticket ). 臭名昭着的LH票#1339( LH票 )。 This bug will only happen when you have cache_classes set to FALSE (development, test).
只有在cache_classes设置为FALSE(开发,测试)时才会发生此错误。
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. 谷歌搜索几周后,我仍然没有找到一个很好的技术来存储AR实例在memcached中,而不必处理上面列出的2个问题。
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. 我还没有尝试过的想法是从实例中删除属性作为字符串(只是AR在进行类型转换之前如何从DB接收它们),将它们存储在memcached中然后从缓存中检索,以某种方式实例化AR使用这些值的对象。 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. 我只是在寻找其他Rails开发人员解决这个问题的方法。
In our projects we store the object as XML. 在我们的项目中,我们将对象存储为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. 序列化/反序列化XML需要额外的成本。 But this has enabled us to share the cache amongst non Ruby apps.
但这使我们能够在非Ruby应用程序之间共享缓存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.