我是否有理由不应该使用 Storable 或 YAML 序列化我的 (Moose) 对象?

发布于 2024-09-29 15:56:14 字数 1023 浏览 6 评论 0原文

我有一些 Moose 对象和一些其他简单的哈希对象(哈希、数组),我想序列化。

起初,我使用了一个简单的方法

my $obj_store_file = nstore($obj);

my $obj = retrieve($obj_store_file);

效果很好。

后来,我发现了 MooseX::StorageKiokuDB。我尝试使用它们来享受它们所具有的一些好处,但是:

  • 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 using MooseX::Storage, this once single object is duplicated and each reference points to another instance of the object. I was told that MooseX::Storage is not appropriate to represent object graphs and that I might want to try KiokuDB.
  • 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 costume KiokuX::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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反话 2024-10-06 15:56:14

我的经验是,这取决于您序列化数据的原因。我喜欢可存储程序状态,包括窗口大小/位置等。我更喜欢使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文