Perl的数据结构的简单,现代,健壮,透明的持久性 [英] Simple, modern, robust, transparent persistence of data structures for Perl

查看:95
本文介绍了Perl的数据结构的简单,现代,健壮,透明的持久性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种解决方案,以透明持久化没有循环引用的Perl数据结构(甚至不是对象,但对象支持将是一个加号).我不太在乎后端,但是我更喜欢JSON.对象的数量将相对较低(数千个哈希引用,每个哈希引用约5个键). 透明"持久性是指我不需要在每次更新内存数据结构时都将更改提交到存储后端.

I'm looking for a solution to transparently persist Perl data structures (not even objects, but object support would be a plus) without circular references. I don't care that much about the backend, but I'd prefer JSON. The number of objects would be relatively low (a few thousands of hashrefs with about 5 keys each). By "transparent" persistence I mean that I don't want to have to commit changes to the storage backend every time I update the in-memory data structure.

理想的代码如下:

my $ds;

...
# load the $ds data structure from 'myfile'

print $ds->{foo}->{bar};  # baz
$ds->{foo}->{bar} = 'quux';

... program dies, but the updated %hash has been persisted automatically in 'myfile'

# in another invocation
print $ds->{foo}->{bar};  # quux

到目前为止,我已经看过了:

So far I've looked at:

  • Dave Rolsky's Perl Object-Oriented Persistence compilation of modules - no updates since 2003
  • brian d foy's MasteringPerl - Chapter 14. Data Serialization - talks about DBM::Deep, a good candidate. I wish there were a clearer difference between serialization and transparent persistence.
  • Persistent - no updates since 2000
  • SPOPS - abandoned since 2004
  • SLOOPS only has one version on CPAN, from 2005
  • Tangram - looks abandoned too
  • Tie::File::AsHash does transparent persistence, but only supports single-level hashes
  • MooseX::Storage, Storable and JSON look nice, but they're only serialization, not persistence frameworks
  • DBIx::Class, Class::DBI, Fey::ORM, ORM, Rose::DB are OO-RDBM mappers, and I'd rather not use a database backend
  • DB_File requires BerkeleyDB
  • KiokuDB seems too complex for the task

我只找到了一个很有前途的模块, DBM :: Deep .代码与示例中的代码相同,您可以使用

I've only found one promising module, DBM::Deep. The code is just like in the example, and you can load the data structure with

my $ds = DBM::Deep->new( "myfile.db" );

不过,格式为二进制.这不是什么大问题,因为我可以使用JSON以人类可读的格式导出它.

The format is binary, though. Not a big problem, since I can use JSON to export it in a human-readable format.

那么,我是否缺少模块,并且我是否可以正确地解决所有问题?

So, am I missing a module, and am I approaching the problem correctly at all?

推荐答案

要实现透明"目标,您将不得不将其抽象到框架中(如chambwez建议)或使用tie d变量每当更新时,它们都会将自己保存到磁盘中. DBM散列以这种方式使用tie,因此DBM::Deep可能是最好的选择.我所知道的所有其他事情都要求您明确地告诉它何时以性能的名义写出数据和/或缓存写操作.

To achieve your "transparency" goal, you're going to have to either abstract it into a framework (as chambwez suggested) or use tied variables which will save themselves to disk whenever they're updated. DBM hashes use tie in this way, so DBM::Deep is probably your best bet; everything else I'm aware of requires you to explicitly tell it when to write data out and/or caches writes in the name of performance.

这篇关于Perl的数据结构的简单,现代,健壮,透明的持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆