Ruby程序的持久哈希表?

前端之家收集整理的这篇文章主要介绍了Ruby程序的持久哈希表?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的 Ruby脚本需要一个小的非结构化数据库.不是sqlite,更像是持久性哈希表的东西可以完美地工作,只要它可以存储基本的Ruby结构(数组,字符串,哈希等 – 所有可序列化的)并且在Ruby脚本崩溃时不会被破坏.

我知道Perl和Tie :: Hash有很多类似的解决方案,所以对于Ruby来说可能有类似的宝石.那是什么宝石?

编辑:据我所知,PStore和yaml解决方案基于读取,解组,重新编组和在每次更改时编写整个数据库.这不仅要求所有它都适合内存,它是O(n ^ 2).所以它们都不是一个特别好的解决方案.

解决方法

也许 FSDB(文件系统数据库)将满足您的需求.

FSDB is a file system data base. FSDB provides a thread-safe,process-safe Database class which uses the native file system as its back end and allows multiple file formats and serialization methods. Users access objects in terms of their paths relative to the base directory of the database. It’s very light weight (the state of a Database is essentially just a path string,and code size is very small,under 1K lines,all ruby).

$sudo gem install fsdb

以下是文档中的示例:

require 'fsdb'

db = FSDB::Database.new('/tmp/my-data')

db['recent-movies/myself'] = ["LOTR II","Austin Powers"]
puts db['recent-movies/myself'][0]              # ==> "LOTR II"

db.edit 'recent-movies/myself' do |list|
  list << "A la recherche du temps perdu"
end
原文链接:https://www.f2er.com/ruby/269431.html

猜你在找的Ruby相关文章