Perl的数据结构的简单,现代,健壮,透明的持久性 [英] Simple, modern, robust, transparent persistence of data structures for 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的 Perl面向对象的持久性模块的编译-自2003年以来没有更新
- brian d foy的 MasteringPerl-第14章.数据序列化 -谈论DBM :: Deep,一个不错的候选人.我希望序列化和透明持久性之间存在更明显的区别.
- 持久-自2000年以来没有更新
- SPOPS -自2004年以来废弃
- SLOOPS 从2005年开始在CPAN上只有一个版本
- Tangram -看起来也被遗弃了
- Tie :: File :: AsHash 具有透明的持久性,但仅支持单级散列
- MooseX :: Storage , JSON 看起来不错,但它们只是序列化,而不是持久性框架
- DBIx :: Class ,费:: ORM , ORM , Rose :: DB 是OO-RDBM映射器,而 DB_File 需要BerkeleyDB
- 对于任务而言,KiokuDB似乎太复杂了
- 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 tie
d 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屋!