Rails 中 DataMapper 对象的替代序列化
我正在 Rails 应用程序中的缓存层上工作,但在缓存原始 DataMapper 对象时遇到了问题。它们似乎附加了很多导致封送失败的东西(我收到一个关于 Marshal 无法序列化 Proc 对象的错误)。
所以我正在考虑编写自己的预序列化和后反序列化方法来进行缓存。具体来说,我将使用以下命令将 DataMapper 对象转换为元组列表:
o = Foo.get(1234)
as_list = o.model.properties.map { |p| [p.name, o.send(p.name)] }
然后缓存该列表。
我的问题是:如何重建 DataMapper 对象,使我能够像使用普通 DataMapper 查询构造的那样使用它?
我天真的 Foo.new(foo=bar, goo=baz) 方法似乎并没有将它与所有外键和其他东西联系起来。
I'm working on on a caching layer in my Rails app and I'm having trouble caching original DataMapper objects. They seem to have a lot of stuff attached that make marshaling fail (I get an error about Marshal being unable to serialize a Proc object).
So I am considering writing my own pre-serialization and post-deserialization methods for caching. Specifically I will turn the DataMapper object into a list of tuples with this:
o = Foo.get(1234)
as_list = o.model.properties.map { |p| [p.name, o.send(p.name)] }
And then cache that list.
My question is: How do I reconstruct the DataMapper object in a way that allows me to use it as it if were constructed by a normal DataMapper query?
My naive approach of Foo.new(foo=bar, goo=baz)
doesn't seem to connect it up with all of the foreign keys and stuff.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一些“有趣的”代码探索之后,我似乎找到了一些有用的东西:
模型上的
load
方法似乎是 get 使用的,而query
似乎是必需的为了获取存储库名称和其他一些东西。After some "fun" code-spelunking I seem to have found something that works:
The
load
method on the model seems to be what get uses and thequery
seems to be required in order to get the repository names and a few other things.