我是否有理由不应该使用 Storable 或 YAML 序列化我的 (Moose) 对象?
我有一些 Moose 对象和一些其他简单的哈希对象(哈希、数组),我想序列化。
起初,我使用了一个简单的方法
my $obj_store_file = nstore($obj);
,
my $obj = retrieve($obj_store_file);
效果很好。
后来,我发现了 MooseX::Storage
和 KiokuDB
。我尝试使用它们来享受它们所具有的一些好处,但是:
MooseX::Storage
似乎重新创建了多次引用的对象。例如,我的一个序列化对象包含一些属性,每个属性都引用另一个对象的同一实例。在序列化之前,所有这些引用显然都是相同的——它们都指向同一个对象。使用MooseX::Storage
进行序列化/反序列化后,这个曾经的单个对象将被复制,并且每个引用都指向该对象的另一个实例。有人告诉我MooseX::Storage
不适合表示对象图,我可能想尝试KiokuDB
。- 我确实这么做了,尽管我觉得
KiokuDB
无法满足我的需求。我不需要数据库可以提供的所有花哨的东西。不幸的是,由于我的一个对象非常大,并且在使用默认值序列化时会占用内存,因此我似乎必须编写一个自定义序列化程序或单独存储其“数据”部分,然后编写一个服装KiokuX::Module
...再次,相当矫枉过正。
所以,我又回到了普通的 Storable 或 YAML。我的问题很简单:是的,KiokuDB 有一些好处(尤其是它维护对象图的事实),也许对 MooseX::Storage 也有一些好处(尽管我不能'确实找不到后者)。但是,鉴于这些好处对我来说并不是真正有用,是否有任何理由不使用 Storable 或 YAML?
换句话说,以这种方式存储(Moose)对象有什么问题吗?这是“非法”吗?
I have a few Moose objects and some other simple hash objects (hashes, arrays) I'd like to serialize.
At first, I used a simple
my $obj_store_file = nstore($obj);
and
my $obj = retrieve($obj_store_file);
This worked well.
Later, I found about MooseX::Storage
and KiokuDB
. I tried using them to enjoy some benefits they have, but:
MooseX::Storage
seemed to recreate objects that are referred multiple times. For example, one of my serialized objects contains a few attributes, which each of them refers to the same instance of another object. Before serialization, all of these reference are obviously the same -- they all point to the same object. After serialization/de--serialization usingMooseX::Storage
, this once single object is duplicated and each reference points to another instance of the object. I was told thatMooseX::Storage
is not appropriate to represent object graphs and that I might want to tryKiokuDB
.- I did, although I felt
KiokuDB
is an overkill for my needs. I don't need all the fancy stuff a DB can offer. Unfortunately, since one of my objects is really large and choaks on memory when serialized using defaults, it seems I have to write a custom serializer or store its 'data' portion separately then write a costumeKiokuX::Module
... again, quite an overkill.
So, I'm back to plain ol' Storable or YAML. My question is simple: yes, there are some benefits for KiokuDB
(especially the fact it maintains an object graph) and perhaps also for MooseX::Storage
(although I couldn't really find any for the latter). But, given these benefits are not really of use to me, is there any reason not to use Storable or YAML?
In other words, is there anything wrong with storing a (Moose) object this way? Is it 'illegal'?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的经验是,这取决于您序列化数据的原因。我喜欢可存储程序状态,包括窗口大小/位置等。我更喜欢使用 YAML 来获取配置数据或您可能想要与应用程序的另一个副本交换的任何数据。 (即用户之间共享——具有不同版本的 Perl 或 Storable 的用户可能无法读取 Storable 文件。)Storable 支持对象图(假设冻结/解冻正确完成)。我不确定 YAML。
My experience is that it depends on why you're serializing data. I like Storable for program state including things like window size/position. I prefer YAML for configuration data or anything you might want to exchange with another copy of the application. (i.e. share between users -- a Storable file might not be readable by a user with a different version of Perl or Storable.) Storable supports object graphs (assuming that the freeze/thaw is done correctly). I'm not sure about YAML.